2012-09-06 52 views

回答

2

見下面鏈接

How can I check if a URL exists via PHP?

或嘗試

$file = 'http://www.domain.com/somefile.jpg'; 
$file_headers = @get_headers($file); 
if($file_headers[0] == 'HTTP/1.1 404 Not Found') { 
    $exists = false; 
} 
else { 
    $exists = true; 
} 

從這裏:http://www.php.net/manual/en/function.file-exists.php#75064

... ...而低於上述帖子,有一個捲曲的解決方案:

function url_exists($url) { 
    if (!$fp = curl_init($url)) return false; 
    return true; 
} 

更新代碼: -

可以在標籤使用SimpleHtmlDom班找到ID或類

看到下面的網址

http://simplehtmldom.sourceforge.net/

http://simplehtmldom.sourceforge.net/manual_api.htm

http://sourceforge.net/projects/simplehtmldom/files/

http://davidwalsh.name/php-notifications

+0

什麼關於連接超時( - 代碼),403s和301s? 我認爲更好的辦法是檢查http響應代碼是200嗎?真假。 –

+0

它不是看是否存在一個url,它是看看中的HREF是否等於「Example.com」 – alexander7567

+0

我有更新我的答案請看看。 –

0

這是我發現的以防其他人需要它也!

$url = "http://example.com/test.html"; 
    $searchFor = "example.com" 
    $input = @file_get_contents($url) or die("Could not access file: $url"); 
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) { 
    foreach($matches as $match) { 
     echo $match[2]; 
     if ($match[2] == $searchFor) 
     { 
      $isMatch = 1; 
     } else { 
      $isMatch= 0; 
     } 
     // $match[0] = A tag 
     // $match[2] = link address 
     // $match[3] = link text 
    } 
    } 

     if ($isMatch) 
     { 
      echo "<p><font color=red size=5 align=center>The page specified does contain your link. You have been credited the award amount!</font></p>"; 
     } else { 
      echo "<p><font color=red size=5 align=center>The specified page does not have your referral link.</font></p>"; 
     } 
+0

[小馬他來....](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Tchoupi