>有谁知道如何从这个preg_match中获取 int?它只向我显示 URI。
$uri = "/user/view/2";
$pattern = "/user/view/[0-9]+";
if(preg_match('#^' . $pattern . '$#', $uri, $matched)) {
print_r($matched);
}
else {
echo "No match!";
}
您的模式中没有捕获组。改变:
$pattern = "/user/view/[0-9]+";
自:
$pattern = "/user/view/([0-9]+)";
它将在$matched[1]
.
你可以使用 T-Regx 然后去
pattern('^/user/view/[0-9]+$')->match($uri)
->forFirst(function (Match $match) {
$int = $match->group(1);
print_r($int);
})
->orReturn("No match!");
而且您不必用#
分隔它!