0

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>.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

What about connection timed outs(- codes), 403s, and 301s ? I think a better way would be to check http response code is 200 ? TRUE : FALSE.
It was not to see if a url exists, it was to see if a HREF in an <A> is equal to "Example.com"
These approaches are sound, but may not work for URLs redirected by the host server. In these cases one may wish to examine the final "Location: ..." entry in the get_headers result.
0

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

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.