开发者

php - parse friendly url

I have url like this

/cp/foo-bar/another-testing

how to parse it with the patt开发者_如何转开发ern

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

results will be

0:foo
1:bar
2:another-testing

I need a global solution to parse all kind of url with a pattern like that. I mean using {0}, {1} flag.


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


Instead of using {0}, {1}, {2}, I offer a new way: using {$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]}";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜