This is code sample that works
$doc->loadHTML($article_header);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
$imgs
takes from $doc
elements with tag name img
and then I do some operations.
Now I want to getElementsByTagName > img OR iframe and then using $img check which element is this and echo if it is iframe or img.
Please modify my code if it is possible.
You can use XPath on your DOMDocument as follows:
$doc->loadHTML($article_header);
$xpath = new DOMXpath($doc);
$imagesAndIframes = $xpath->query('//img | //iframe');
$length = $imagesAndIframes->length;
for ($i = 0; $i < $length; $i++) {
$element = $imagesAndIframes->item($i);
if ($element->tagName == 'img') {
echo 'img';
} else {
echo 'iframe';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With