php - 解析友好的 url

发布于 2024-09-14 01:14:22 字数 260 浏览 9 评论 0原文

我有这样的 url,

/cp/foo-bar/another-testing

如何使用模式

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

结果解析它,

0:foo
1:bar
2:another-testing

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

I have url like this

/cp/foo-bar/another-testing

how to parse it with the pattern

/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2024-09-21 01:14:22
if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)#'), $url, $matches)) {
    //look into $matches[1], $matches[2] and $matches[3]
}
if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)#'), $url, $matches)) {
    //look into $matches[1], $matches[2] and $matches[3]
}
舂唻埖巳落 2024-09-21 01:14:22

我提供了一种新方法,而不是使用 {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]}";

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]}";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文