为什么preg_match_all返回int(376)而不是电话号码列表?

时间:2014-12-24 03:52:45

标签: php regex

我使用正则表达式来获取电话号码,但是当我使用var_dump而不是电话号码列表时,最终得到int(376)。任何帮助或回复将不胜感激。谢谢

<?php

$html=file_get_contents("http://www.senaraiscammer.com/");

$match='/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/';

$formatted=preg_match_all($match,$html);

//print_r($formatted);  // output is => 376 

var_dump($formatted);  // output is =>  int(376)
?>

2 个答案:

答案 0 :(得分:2)

RTM

  

返回值

     

返回完整模式匹配的数量(可能为零),如果发生错误则返回FALSE。

正确使用将是:

preg_match_all($match,$html,$phones);  // This third parameter contains matches
print_r($phones);

答案 1 :(得分:0)

您将返回匹配计数而非实际匹配结果。

正确的语法是:

preg_match_all($match, $html, $matches);
var_dump($matches[0]);

Working Demo