php 使用 preg_match 从字符串中获取数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15408673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get numbers from string using preg_match
提问by user2169679
I have a string :
我有一个字符串:
<div id="post_message_957119941">
I want to fetch only the numbers (957119941) from this string using preg_match.
我只想使用preg_match.
回答by w00
This shouldn't be too hard.
这应该不会太难。
$str = '<div id="post_message_957119941">';
if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) )
{
print_r($matches);
}
Output:
输出:
Array ( [0] => post_message_957119941 [1] => 957119941 )
Array ( [0] => post_message_957119941 [1] => 957119941 )
So the desired result will always be in: $matches[1]
所以想要的结果总是在: $matches[1]
Is that what you need?
那是你需要的吗?

