开发者

How to search html file for simple string?

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:开发者_如何学Go//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?


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
}


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.


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"');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜