preg_match 返回完整字符串

Anu*_*nup 0 php preg-match-all preg-match

我正在尝试从字符串中获取数字,但我不断获取带有数字的完整字符串,而不是单独的数字

$string = "stuff here with id=485&other=123";
preg_match('(id=\d+)',$string,$match);
Run Code Online (Sandbox Code Playgroud)

上面的结果就像 id=485 但我只想要 485

任何帮助将不胜感激。

Pop*_*les 5

括号表示要收集的内容。您还需要在两端使用分隔符

preg_match('/id\=(\d+)/',$string,$match);
print_r($match);

/* should be
0=>"id=485",
1=>"485"
*/

echo $match[1];
Run Code Online (Sandbox Code Playgroud)

编辑:参见 cryptic 的答案,parse_str 很可能比 preg_match 更快。大多数事情都是。