0

有谁知道如何从这个 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!";
}
4

2 回答 2

2

您的模式中没有捕获组。改变:

$pattern = "/user/view/[0-9]+";

至:

$pattern = "/user/view/([0-9]+)";

它会在$matched[1].

于 2013-11-04T18:46:16.520 回答
0

你可以使用T-Regx

 pattern('^/user/view/[0-9]+$')->match($uri)
     ->forFirst(function (Match $match) {
         $int = $match->group(1);
         print_r($int);
     })
     ->orReturn("No match!");

而且您不必用#!

于 2018-10-09T13:57:44.667 回答