php-解析友好url

php-解析友好url,php,regex,friendly-url,Php,Regex,Friendly Url,我有这样的url /cp/foo-bar/another-testing 如何用模式解析它 /cp/{0}-{1}/{2} 结果将是 0:foo 1:bar 2:another-testing 我需要一个全局解决方案来解析所有类型的url,并使用这样的模式。我的意思是使用{0},{1}标志。而不是使用{0},{1},{2},我提供了一种新的方法:使用{$s[0]},{$s[1]},{$s[2]}: if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)

我有这样的url

/cp/foo-bar/another-testing
如何用模式解析它

/cp/{0}-{1}/{2}
结果将是

0:foo
1:bar
2:another-testing

我需要一个全局解决方案来解析所有类型的url,并使用这样的模式。我的意思是使用{0},{1}标志。

而不是使用
{0}
{1}
{2}
,我提供了一种新的方法:使用
{$s[0]}
{$s[1]}
{$s[2]}

if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)#'), $url, $matches)) {
    //look into $matches[1], $matches[2] and $matches[3]
}
$your_url = '/cp/foo-bar/another-testing';
$s = explode('/', $your_url);
if(!$s[0])
     array_shift($s);
if($temp = array_pop($s))
     $s[] = $temp;
//then
$result = "/cp/{$s[0]}-{$s[1]}/{$s[2]}";

我没有使用
{0}
{1}
{2}
,而是提供了一种新的方法:使用
{$s[0]}
{$s[1]}
{$s[2]}

$your_url = '/cp/foo-bar/another-testing';
$s = explode('/', $your_url);
if(!$s[0])
     array_shift($s);
if($temp = array_pop($s))
     $s[] = $temp;
//then
$result = "/cp/{$s[0]}-{$s[1]}/{$s[2]}";

如果它将是一个全局解决方案,那么另一个测试的解析为何会不同?如果它将是一个全局解决方案,那么另一个测试的解析为何会不同?嗨,#字符的意思是什么?@Stone它只是一个分隔符。您好,#字符是什么意思?@Stone它只是一个分隔符。看见