這是我想做的事..PHP - 檢查URL鏈接中存在
比方說,我在尋找一個文件的鏈接「example.com」在http://example.com/test.html」
我想要的。在上面提到的網站上找一個PHP腳本,但是如果在<A>
中有類或ID標籤,我也需要它。
這是我想做的事..PHP - 檢查URL鏈接中存在
比方說,我在尋找一個文件的鏈接「example.com」在http://example.com/test.html」
我想要的。在上面提到的網站上找一個PHP腳本,但是如果在<A>
中有類或ID標籤,我也需要它。
見下面鏈接
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
這是我發現的以防其他人需要它也!
$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>";
}
[小馬他來....](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Tchoupi
什麼關於連接超時( - 代碼),403s和301s? 我認爲更好的辦法是檢查http響應代碼是200嗎?真假。 –
它不是看是否存在一個url,它是看看中的HREF是否等於「Example.com」 – alexander7567
我有更新我的答案請看看。 –