Php preg_match和RegEx从HTML页面查找数字
使用preg_match函数,我想从下面的html字符串中找到数字30899 我使用了以下代码行:Php preg_match和RegEx从HTML页面查找数字,php,regex,preg-match,Php,Regex,Preg Match,使用preg_match函数,我想从下面的html字符串中找到数字30899 我使用了以下代码行: preg_match("/<a ?.* onclick=\"cms4i.models.dealersearch.view.navigate\('DETAIL', '(.*)'\);\" class=\"partner_name link\">Zentrum GmbH Standort<\/a>/i", $data, $matches); preg_匹配(“/ 合伙人(Ve
preg_match("/<a ?.* onclick=\"cms4i.models.dealersearch.view.navigate\('DETAIL', '(.*)'\);\" class=\"partner_name link\">Zentrum GmbH Standort<\/a>/i", $data, $matches);
preg_匹配(“/
合伙人(Verkauf)
合伙人(服务)
弗兰克林斯特拉城2410587
5.4公里
虽然我之前解决了这类问题,但我找不到解决方案如何从这个字符串中找到这些数字。我相信专家可以很快找到解决方案。那怎么办
preg_match('/<a (.*) onclick=/', $data);
preg_match('/正如我看到的,您正在搜索作为navigate
方法中的参数的数字。因此,您可以尝试使用:
preg_match_all('/\.navigate\([a-zA-Z\,\s\']+([0-9]{3,})\'\);" class="partner_name link"\>Zentrum GmbH Standort\<\/a\>/', $html, $matches);
print_r($matches[1]);
如果可能有多个唯一的数字,那么您可以排除重复的数字。如果没有,则只提取一个数字,而不是全部。首先,您需要逃出句点,否则,它对您是否有效?或者您是否希望获得所有的30899
?是的,您是对的。现在,我使用\来逃出句点。但是,它确实有效esn不返回任何匹配的字符串。我猜这就是函数。也许除了您已经使用的I
标志之外,您还需要使用g
标志。或者像enenen建议的那样使用preg\u match\u all
。谢谢。是的,它会返回那些标记上显示的所有不同数字。我如何找到接近的数字?”Zentrum GmbH Standort”。请注意,整个html页面包含数百个不同的数字。我想使用“class=”partner\u name link“>Zentrum GmbH Standort”查找特定的数字。我编辑了我的答案。但如果您想在html元素中/附近使用数字,则最好使用DOMDocument而不是正则表达式。
preg_match_all('/\.navigate\([a-zA-Z\,\s\']+([0-9]{3,})\'\);" class="partner_name link"\>Zentrum GmbH Standort\<\/a\>/', $html, $matches);
print_r($matches[1]);
Array
(
[0] => 30899
[1] => 30899
[2] => 30899
)