繁体   English   中英

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

[英]PHP - Check if URL exists in link

这是我想要做的..

假设我在http://example.com/test.html上的文件中寻找链接“example.com”。

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

见下面的网址

如何通过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或类

请参阅以下网址

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>";
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM