我有一个 PHP 脚本,它在使用 CURL_MULTI 函数下载的页面上查找链接。下载很好,我得到了数据,但是当我遇到一个将 url 列为非链接的页面时,我的脚本随机崩溃。这是代码:
$fishnof = strpos($nofresult, $supshorturl, 0);
$return[0] = ''; $return[1] = ''; // always good to cleanset
// Make sure we grabbed a link instead of a text url(no href)
if ($fishnof !== false) {
$linkcheck = rev_strpos($nofresult,'href',$fishnof);
$endthis = false;
while($endthis !== true) {
if($linkcheck > ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
$endthis = true;
break;
}
$lastfishnof = $fishnof;
$fishnof = strpos($nofresult,$supshorturl,$fishnof+1);
if($fishnof === false){$fishnof = $lastfishnof;$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;}// This is the last occurance of our URL on this page
if($linkcheck > $fishnof){$linkcheck = rev_strpos($nofresult,'href',$fishnof);$endthis = true;break;} // We went around past the end of the string(probably don't need this)
$linkcheck = rev_strpos($nofresult,'href',$fishnof);
}
if($linkcheck < ($fishnof - 25)){ // 19 accounts for href="https://blog. 25 just in case
$return[0] = 'Non-link.';
$return[1] = '-';
$nofresult = NULL; // Clean up our memory
unset($nofresult); // Clean up our memory
return $return;
}
}
这是自定义的 rev_strpos,它只是做一个反向操作strpos()
:
// Does a reverse stripos()
function rev_strpos(&$haystack, $needle, $foffset = 0){
$length = strlen($haystack);
$offset = $length - $foffset - 1;
$pos = strpos(strrev($haystack), strrev($needle), $offset);
return ($pos === false)?false:( $length - $pos - strlen($needle) );
}
因此,如果:
$nofresult = '
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
google.com Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.
<a href="http://www.google.com">Google</a> Some text.Some text.
Some text.Some text.Some text.Some text.Some text.Some text.';
和
$supshorturl = "google.com";
这应该找到 google.com 的第二次出现的位置,它位于 HTML href 标记内。问题是它在崩溃之前没有报告任何错误,我的错误设置:
ini_set("display_errors", 1);
error_reporting(E_ALL & ~E_NOTICE);
set_error_handler('handle_errors');
我的handle_errors()
函数将所有错误记录在一个文件中。但是,在脚本崩溃之前不会报告任何错误。我的 curl_multi 还处理许多 URL,有时它会在某个 URL 上崩溃,有时它会在另一个 URL 上崩溃......我准备拔掉头发,因为这看起来很容易......但在这里我是。另一个注意点是,如果我删除了 while 循环,那么不会崩溃,如果页面首先在 href 标记中包含 url,那么它也不会崩溃。请帮我弄清楚这件事。太感谢了!