必须修复您的正则表达式以匹配 176,因为您现有的正则表达式仅匹配第一个数字。
<?php
$patterns = array(
'([a-z]*)\.html' => 'category',
'^([0-9]*)-([a-zA-Z]*)' => 'article/$1'
);
$in = '176-title-of-article';
$out = null;
foreach ($patterns as $pattern => $replacement) {
if (preg_match('/' . str_replace('/', '\\/', $pattern) . '/', $in, $match)) {
$out = preg_replace_callback('/\$(\d+)/', function ($placeholder) use ($match) {
if (!isset($match[$placeholder[1]])) {
throw new Exception("route replacement contains invalid capture group '\${$placeholder[1]}'.");
}
return $match[$placeholder[1]];
}, $replacement);
break;
}
}
var_dump($out);