Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在正则表达式中执行此操作?_Php_Regex - Fatal编程技术网

Php 如何在正则表达式中执行此操作?

Php 如何在正则表达式中执行此操作?,php,regex,Php,Regex,我正在用几种语言构建一个应用程序 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/)。我希望能够根据传递的u

我正在用几种语言构建一个应用程序

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确定语言

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
编辑:好的,我想现在可以了。不过,可能有一个更优雅的解决方案。第一组是语言代码,第二组是页面。您提供的四个输入对我有效。

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

preg_match('^/((ca|fr|es)(/|$))?(.*)$', $url, $matches);
$lang = $matches[2];
if (!$lang) {
  $lang = 'en';
}
$url = $matches[4];
说明:

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

编辑:好的,我想现在可以了。不过,可能有一个更优雅的解决方案。第一组是语言代码,第二组是页面。你提供的四个输入对我很有用。

不用麻烦,我们的英语读得很好。我不明白你的问题是什么。你想用正则表达式匹配什么?很抱歉。实际上有4种语言/fr/和/es/我希望能够从传递的url确定语言。好的,显然你必须能够区分什么是页面名称和什么是语言,否则你将在正则表达式中枚举所有可能的语言。我们能假设在
test.com/
之后出现的任何两个字母的组合都是一种语言吗?或者您希望有一个页面“xr”,例如?我只有/es//ca//fr/(希望)可以过滤掉。我遇到的问题是,如果url以相同的两个字母开头,并使用其中一种语言,即/fr/-/french promo/不用麻烦,我们的英语读得很好。我不明白你的问题是什么。你想用正则表达式匹配什么?很抱歉。实际上有4种语言/fr/和/es/我希望能够从传递的url确定语言。好的,显然你必须能够区分什么是页面名称和什么是语言,否则你将在正则表达式中枚举所有可能的语言。我们能假设在
test.com/
之后出现的任何两个字母的组合都是一种语言吗?或者您希望有一个页面“xr”,例如?我只有/es//ca//fr/(希望)可以过滤掉。我遇到的问题是,如果url以相同的两个字母开头,并使用其中一种语言,即/fr/-/french promo/Whoops,那么看起来你修复了你的,就像我修复了我的一样;就像我的一样,当语言后面没有斜杠时,你的第一次失败了。我更喜欢你的;更容易不使用正向前瞻,只需匹配+消耗斜杠。哎哟,看起来你在修复你的斜杠,就像我在修复我的斜杠一样;就像我的一样,当语言后面没有斜杠时,你的第一次失败了。我更喜欢你的;更容易不使用正向前瞻,只需匹配+使用斜杠。
preg_match('^/((ca|fr|es)(/|$))?(.*)$', $url, $matches);
$lang = $matches[2];
if (!$lang) {
  $lang = 'en';
}
$url = $matches[4];