【发布时间】:2015-12-10 01:14:02
【问题描述】:
我有一个字符串数组:
$routes = Array
(
[0] => Array
(
[0] => /
)
[1] => Array
(
[0] => /articles/[:slug]?
)
[2] => Array
(
[0] => /articles/[:cid]/[:slug]?
)
[3] => Array
(
[0] => /articles/[a:year]/?[*:month]?/?[*:day]?
)
)
还有一个带有下面参数的数据数组。根据这些数据,我想从路线中找到最佳匹配。
Array
(
[year] => 2012
[day] => 11
[month] => 01
)
在上面的例子中:我想获得 $routes[3]。
我尝试过这样的事情:
foreach($routes as $route) {
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route[0], $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
list($block, $pre, $type, $param, $optional) = $match;
// How to check???
}
}
}
【问题讨论】:
-
1) 你有没有尝试过一些事情来达到你的目标? 2) 为什么元素 3 是“最佳匹配”?
-
我尝试了一些方法但没有成功。
-
你应该把它包括在内以帮助潜在的答案。
-
请用我试过的代码检查我更新的问题
标签: php arrays regex preg-replace