我有一個PHP腳本,它查找與CURL_MULTI函數一起下載的頁面上的鏈接。下載是好的,我得到的數據,但我的腳本隨機崩潰,當我遇到一個頁面被列爲非鏈接的網址。這是代碼:PHP strpos()崩潰腳本
$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上崩潰,而有時它會在另一個網址上崩潰......我準備好拉出我的頭髮,因爲這看起來像是一個簡單的交易......但在這裏,我上午。另一點需要注意的是,如果我刪除了while循環,則不會崩潰,如果頁面首先在href標記中包含url,那麼它不會崩潰。請幫我把這件事情弄清楚。太感謝了!
如何使用正則表達式來獲取網址? – yoda
我需要在頁面上該URL的位置,而不是URL本身。 –
問題是?您可以完美地使用回調來執行您想要的任何操作。 – yoda