Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用preg_匹配从字符串中获取数字_Php_Preg Match - Fatal编程技术网

Php 使用preg_匹配从字符串中获取数字

Php 使用preg_匹配从字符串中获取数字,php,preg-match,Php,Preg Match,我有一个字符串: <div id="post_message_957119941"> 我只想使用preg\u match从这个字符串中获取数字(957119941) 这应该不会太难 $str = '<div id="post_message_957119941">'; if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) ) { print_r($matches); } $str='

我有一个字符串:

 <div id="post_message_957119941">


我只想使用
preg\u match
从这个字符串中获取数字(957119941)

这应该不会太难

$str = '<div id="post_message_957119941">';

if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) )
{
    print_r($matches);
}
$str='';
if(preg_match('/post_message([0-9]+)/',$str,$matches))
{
打印(匹配项);
}
输出:

Array([0]=>post\u message\u 957119941[1]=>957119941)

因此,所需的结果总是:
$matches[1]


这就是你需要的吗?

你试过我的正则表达式了吗?它对你有用吗?我不明白为什么这个问题因“不是真正的问题”而关闭。。。好吧:preg_match(“/\d+/”,“”,$match)将设置$match[0=>957119941]我需要多个字符串和数字,然后我需要用户
if(preg_match('/S([0-9]+)E([0-9]+)/,strtoupper($fileName),$matches))
这对我有用