PHP - 检查链接中是否存在URL

时间:2012-09-06 19:23:18

标签: php html url

这就是我想要做的事情..

让我们说我在http://example.com/test.html“的文件中寻找”example.com“链接。

我想在上面提到的网站上找一个PHP脚本。但是,如果<A>中有类或ID标记,我也需要它才能工作。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>";
      }
相关问题