2010-08-14 59 views
0

我有这样PHP - 解析友好的URL

/cp/foo-bar/another-testing 

网址如何与模式解析它

/cp/{0}-{1}/{2} 

结果将

0:foo 
1:bar 
2:another-testing 

我需要一个全球性的解决方案用类似的模式解析所有类型的网址。我的意思是使用{0},{1}标志。

+0

如果它要成为一个全球性的解决办法,怎么来的另一个测试是解析不同? – stillstanding 2010-08-14 19:06:30

回答

2
if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)#'), $url, $matches)) { 
    //look into $matches[1], $matches[2] and $matches[3] 
} 
+0

嗨,#字符是什么意思? – StoneHeart 2010-08-16 14:51:12

+0

@Stone它只是一个分隔符。请参阅http://pt.php.net/manual/en/regexp.reference.delimiters.php – Artefacto 2010-08-16 15:07:44

1

而不是使用{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]}";