在php中用正则表达式获取数字

时间:2013-04-27 16:18:26

标签: php regex extraction

我正在尝试从页面中提取所有数字。 该页面如下所示:

....lots of html code ....
<script>
..some code...
["listidname",[],{"list":["123456","96326478664","12345678901234"]},12]
...more code....
</script>
...even more code...

列表中的数字量可能会有所不同,最后的12也只是一个随机数,所以这也会有所不同。

我想要做的是提取123456,96326478664和12345678901234。 然而,我不是很强大的PHP,更不用说正则表达式..

preg_match_all("/(\d+)/", $input, $output);

给我数字,还有页面上的所有其他数字......

任何人都可以帮我吗? 谢谢。

2 个答案:

答案 0 :(得分:1)

如果数字是双引号,请尝试

preg_match_all("/\"(\d+)\"/", $input, $output);

答案 1 :(得分:0)

您必须首先提取该行,然后找到数字:

if (preg_match('~\["listidname",\[],\{"list":(?:[[,]"\d++")++]},\d++]~', $html, $match)) {
    preg_match_all('~"\K\d++~', $match[0] ,$result);
    print_r($result);
}