【问题标题】:php - parse friendly urlphp - 解析友好的 url
【发布时间】:2011-03-29 22:03:45
【问题描述】:

我有这样的网址

/cp/foo-bar/another-testing

如何用模式解析它

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

结果将是

0:foo
1:bar
2:another-testing

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

【问题讨论】:

  • 如果要成为一个全局解决方案,那么 another-testing 的解析方式为何不同?

标签: php regex friendly-url


【解决方案1】:
if (preg_match('#/cp/([^/]+?)-([^/]+?)/([^/]+)#'), $url, $matches)) {
    //look into $matches[1], $matches[2] and $matches[3]
}

【讨论】:

【解决方案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]}";

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2014-09-18
    • 2016-04-13
    • 2010-11-01
    • 2016-06-02
    • 2020-07-23
    • 2011-05-09
    • 2018-05-22
    相关资源
    最近更新 更多