【发布时间】:2014-08-17 04:33:23
【问题描述】:
我有这段代码,每当它运行时,它都会执行两次。这是一个问题,因为我需要为用户显示此信息。
我对数组不太熟悉,因此我希望您能在这方面拥有专业知识。
我的代码如下:
$wwwLink = "http://agynix.com/";
$IMEIapproved = false;
$data = file_get_contents($wwwLink);
//In the pattern the "class" is the div class, and the "div-res" is the value of the class
$pattern = '%<div\b[^>]*?\bclass\s*+=\s*+([\'"]?+)\bdiv-res\b(?(1)\1)[^>]*+>((?:[^<]++|<(?!/?div\b|!--)|<!--.*?-->|<div\b[^>]*+>(?2)</div\s*>)*+)</div\s*>%isx';
$matchcount = preg_match_all($pattern, $data, $matches);
if ($matchcount > 0) {
print_r($matches);
}
else {
echo "Holy shit! Back to the drawing board";
}
我的问题是;如何使此代码仅检索一次信息?是echo/print_r,是数组信息,是模式,还是我的无能?
【问题讨论】:
-
如果代码确实运行了两次,这是在外部完成的,您必须提供调用代码。如果您将
$matchcount称为>1,这取决于网站为您提供的数据以及您使用的正则表达式。在这种情况下,什么都不会运行两次。 -
agynix.com 上没有
div-res类的元素。事实上-res在源代码中没有出现。 -
我知道没有...抱歉,我更改了链接但忘记更改类值。
-
@urzeit 如果我将 >0 更改为 >1,它将显示错误消息。如果有人可以请提供一些示例代码?将不胜感激。 :-)
标签: php file-get-contents preg-match-all