federk federk - 7 months ago 10
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();
$content->loadHTML($htmlcontent);
$imgTags=$content->getElementsByTagName('img');
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)?

Answer

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();
$content->loadHTML($htmlcontent);

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

Output:

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