我正在使用多种语言构建应用。
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
我该如何过滤?
抱歉,抱歉。实际上有4种语言(/ fr /和/ es /)。我希望能够从传递的url中确定语言。答案 0 :(得分:2)
test.com/(?:([a-z]{2})(?=$|/))?(?:/)?(.*)
说明:
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
编辑:好的,我认为现在可行了。可能会有更优雅的解决方案。第一组是语言代码,第二组是页面。通过你提供的四个输入,它对我有用。
答案 1 :(得分:1)
preg_match('^/((ca|fr|es)(/|$))?(.*)$', $url, $matches);
$lang = $matches[2];
if (!$lang) {
$lang = 'en';
}
$url = $matches[4];