alexander7567
alexander7567

Reputation: 664

PHP - Check if URL exists in link

Here is what I want to do..

Lets say I am looking for the link "example.com" in a file at http://example.com/test.html".

I want to take a PHP script that looks for an in the mentioned website. However, I also need it to work if there is a class or ID tag in the <A>.

Upvotes: 0

Views: 1060

Answers (2)

Abid Hussain
Abid Hussain

Reputation: 7762

See below url

How can I check if a URL exists via PHP?

or try it

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

From here: http://www.php.net/manual/en/function.file-exists.php#75064

...and right below the above post, there's a curl solution:

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

Update code:-

You can use SimpleHtmlDom Class for find id or class in tag

see the below URL

http://simplehtmldom.sourceforge.net/

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

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

http://davidwalsh.name/php-notifications

Upvotes: 2

alexander7567
alexander7567

Reputation: 664

Here is what I have found in case anyone else needs it also!

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

Upvotes: 0

Related Questions