How to search html file for simple string?

10,075

Solution 1

You could use strpos():

$url = "http://www.example.com/";
$html = file_get_contents($url);
if (strpos($html, "http://ecx.images-amazon.com/images/I/41UQmT7-XyL.jpg") !== false) {
  // found
} else {
  // not found
}

Solution 2

If you just want to know if a particular string is present or not, use strpos():

if (strpos($html_goes_here, 'http://ecx.blahblah.jpg') !== FALSE)) {
   ... image is present ...
}

Note the use of the strict comparison operator, as per the warnings on the linked documentation page.

Solution 3

I mixed params in my comment and you wanted to know how to load the HTML of the URL:

$url = "http://rads.stackoverflow.com/amzn/click/B00519RW1U";
$html = file_get_contents($url);
$found = false !== strpos($html, 'src="http://ecx.images-amazon.com/images/I/41UQmT7-XyL.jpg"');
Share:
10,075
Shorty.
Author by

Shorty.

Updated on June 04, 2022

Comments

  • Shorty.
    Shorty. almost 2 years

    Consider this link from Amazon.

    If you notice, each seller has this block (similar, at least):

    <a href="http://www.amazon.com/shops/AN8LN2YPKS7DF/ref=olp_merch_name_2">
    <img src="http://ecx.images-amazon.com/images/I/41UQmT7-XyL.jpg" width="120" alt="DataVision Computer Video" height="30" border="0" />
    </a> //and other junk
    

    I want to search this page for http://ecx.images-amazon.com/images/I/41UQmT7-XyL.jpg, which is the seller image (which I already have the link for). I just want to know if the search produced results, or not. I don't even need to know more than that. Is this possible? How can I do it using PHP?

  • Shorty.
    Shorty. over 12 years
    How do I download the html to put in the variable?
  • ldiqual
    ldiqual over 12 years
    $html = file_get_contents($url)
  • Shorty.
    Shorty. over 12 years
    Weird, getting this error: Parse error: syntax error, unexpected T_IF in...
  • DaveRandom
    DaveRandom over 12 years
    You have probably missed a semi-colon or a closing bracket off the line before the line number in the error message...
  • Shorty.
    Shorty. over 12 years
    Yes I did. Now I'm getting Parse error: syntax error, unexpected ')' . Doesn't look like I'm missing a closing parentheses, though.
  • DaveRandom
    DaveRandom over 12 years
    Missing semi-colon, and why are you executing a url as a system command? ;-)
  • Shorty.
    Shorty. over 12 years
    Thank you. However, you really messed up the first line. Should be in quotes "" instead of backticks, and missing the semicolon.