view raw
federk federk - 1 year ago 35
HTML Question

Is it possible to extract full accurate image tag from a html code using DOM in php?

I'm trying to get full accurate img tags from a html code using DOM:

$content=new DOMDocument();
foreach($imgTags as $tag) {
echo $content->saveXML($tag); }

If i had the original
<img src="img">
, the result would be
<img src="img"/>
. But i need the exact value corresponding to the original.

It is possible - to get the exact img tag using DOM without regular expressions or thirdparty libraries (Simple HTML DOM)?


No. It isn't possible to do this.

However, you can achieve your goal of removing the <img> elements from an HTML document if they meet specific conditions using DOMDocument. Here's some sample code which removes images which contain the class attribute "removeme".

$htmlcontent =
    '<!DOCTYPE html><html><head><title>Example</title></head><body>'
    . '<img src="1"><img src="2" class="removeme"><img src="3"><img class="removeme" src="4">'
    . '</body></html>';

$content=new DOMDocument();

foreach ($content->getElementsByTagName('img') as $image) {
    if ($image->getAttribute("class") == "removeme") {
echo $content->saveHTML();


<!DOCTYPE html> <html><head><title>Example</title></head><body><img src="1"><img src="3"></body></html>