使用preg_match从字符串中获取数字

时间:2013-03-14 11:59:51

标签: php preg-match

我有一个字符串:

 <div id="post_message_957119941">

我想使用preg_match从此字符串中仅获取数字(957119941)。

1 个答案:

答案 0 :(得分:23)

这不应该太难。

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

if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) )
{
    print_r($matches);
}

输出:

Array ( [0] => post_message_957119941 [1] => 957119941 )

因此,所需的结果将始终位于:$matches[1]

这就是你需要的吗?