You could use XPath to parse the html, and pull out the data you want that way. It's a little more involved than string position checking, but has the advantage of being a bit more robust should you decide that you want something more specific (src
and alt
of first img
tag, for example).
First you load the html string in to a DOMDocument, which is then loaded in to XPath.
// Load html in to DOMDocument, set up XPath
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
We want the first img
that occurs on the page, so use the selector /descendant::img[1]
. N.B, this is not the same as //img[1]
, though that may often give similar results. There's a good explanation here on the difference between the two.
$matches = $xpath->evaluate("/descendant::img[1]");
A downside of using XPath is that it doesn't make it easy to say "give me back the full string that was matched for that img
tag", so we can put together a simple function that'll iterate over the matched node's attributes and re-build an img
tag.
$tag = "<img ";
foreach ($node->attributes as $attr) {
$vals[] = $attr->name . '="' . $attr->value . '"';
}
$tag .= implode(" ", $vals) . " />";
Putting it all together we get something like:
<?php
// Example html
$html = '<html><body>'
. ' <img src="/images/my-image.png" alt="My image" width="100" height="100" />'
. 'Some text here <img src="do-not-want-second.jpg" alt="No thanks" />';
// Load html in to DOMDocument, set up XPath
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
// Get the first img in the doc
// N.B. Not the same as "//img[1]" - see https://stackoverflow.com/a/453902/2287
$matches = $xpath->evaluate("/descendant::img[1]");
foreach ($matches as $match) {
echo buildImgTag($match);
}
/**
* Build an img tag given it's matched node
*
* @param DOMElement $node Img node
*
* @return Rebuilt img tag
*/
function buildImgTag($node) {
$tag = "<img ";
$vals = array();
foreach ($node->attributes as $attr) {
$vals[] = $attr->name . '="' . $attr->value . '"';
}
$tag .= implode(" ", $vals) . " />";
return $tag;
}
```
So overall it's a slightly more complex approach than doing a strpos
or regex on the html, but should provide you with more flexibility should you decide to do anything with the img
tag, like pulling out a specific attribute.