tags:

views:

58

answers:

2

I'm building an app with several languages.

test.com/ -english index
test.com/ca - canadian english index
test.com/canada-promo - english version of canada promo page
test.com/ca/canada-promo - canadian english version of promo page

how would I filter this?

sorry about that. there are actually 4 languages (/fr/ and /es/). I want to be able to determine the language from the url that is passed.

+2  A: 

test.com/(?:([a-z]{2})(?=$|/))?(?:/)?(.*)

Explanation:

test.com/  #match beginning boilerplate, replace with "^" if need be
(?:([a-z]{2})(?=$|/))?  #match two characters if followed by the end of line or slash
(?:/)? #consume the slash if there was one
(.*)   #the page

Edit: OK I think this works now. There may be a more elegant solution though. First group is the language code, second group is the page. It works for me with the four inputs you gave.

Mark Peters
+1  A: 
preg_match('^/((ca|fr|es)(/|$))?(.*)$', $url, $matches);
$lang = $matches[2];
if (!$lang) {
  $lang = 'en';
}
$url = $matches[4];
ash108
Whoops looks like you fixed yours as I was fixing mine; like mine yours at first failed when the language wasn't followed by a slash. I like yours better; easier not to use the positive lookahead and just match+consume the slash.
Mark Peters