这是我想要做的。
比方说,我正在一个位于的文件中查找链接"example.com"http://example.com/test.html".
我想用一个PHP脚本,在上面提到的网站上查找。但是,如果<A>
中有类或ID标记,我也需要它来工作。
请参阅下面的url
如何通过PHP检查URL是否存在?
或者试试
$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或类
请参阅以下URL
http://simplehtmldom.sourceforge.net/
http://simplehtmldom.sourceforge.net/manual_api.htm
http://sourceforge.net/projects/simplehtmldom/files/
http://davidwalsh.name/php-notifications
以下是我的发现,以防其他人也需要它!
$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>";
}