How to replace HTML content of a div using DOMElement from php

Question:

I'm trying this way

libxml_use_internal_errors(true);
//pegar o conteudo de um pagina web
$doc = new DomDocument();
$doc->loadHTMLFile('http://www.astralsaudeambiental.com.br/');

$div = $doc->getElementById('sidebar');
$string_div = '';
foreach($div->childNodes as $node) {
   $string_div .= $doc->saveHTML($node);
}
if(!$div)
{
    die("Element not found");
}
//alterar o conteudo de um arquivo HTML em determinada div 
$content_doc = new DomDocument();
$content_doc -> loadHTML('<html><head><title>titulo</title></head><body><div                 style="width:200px;height:200px;background:#000000;"></div><div id="sidebar"></div></body>    </html>');
$content_doc -> getElementById('sidebar')->nodeValue = $string_div;
$content_doc_div = $content_doc -> getElementById('sidebar');
$content_doc_div_string = ''; 
foreach($content_doc_div->childNodes as $node) {
   $content_doc_div_string .= $content_doc->saveHTML($node);
}

echo $content_doc -> saveHTML();

However, the content is inserted into the HTML file so that it appears as text

                &lt;div class="unidades box clear"&gt;
                &lt;h2&gt;Encontre a unidade mais próxima de você!&lt;/h2&gt;
                &lt;div&gt;
                    &lt;p&gt;cConteudo.&lt;br&gt;
                    Use o campo abaixo para buscar a&lt;br&gt;
                    unidade mais próxima da sua cidade:&lt;/p&gt;
                    &lt;p&gt;&lt;/p&gt;

Answer:

To solve this issue, using only DOMDocument, you must use importNode , because it is two different DOMDocument, the link to this documentation has the solution to your problem, below is an example of it already implemented with the code you passed. I just didn't use foreach, as it already replaces directly, but if you want you can loop with a repeat command, store the node in an array and then loop through again and replace or add . You can remove nodes you don't want too.

<?php

libxml_use_internal_errors(true);


$doc = new DOMDocument;
// Pegar o conteudo do side 1, um pagina web
$doc->loadHTMLFile('http://www.astralsaudeambiental.com.br/');
// Pegar o elemento com id sidebar
$doc1Sidebar = $doc->getElementById('sidebar');

// Pegar o conteúdo do site 2
$doc2 = new DOMDocument;
$doc2->loadHTML('<html>'
        . '<head>'
        . '<title>titulo</title>'
        . '</head>'
        . '<body>'
        . '<div style="width:200px;height:200px;background:#000000;"></div>'
        . '<div id="sidebar">'
        . '<div></div>'
        . '</div>'
        . '</body>'
        . '</html>');

$doc2Sidebar = $doc2->getElementById('sidebar');
// Importa o node
$newNode = $doc2->importNode($doc1Sidebar, true);
// Substitui o node
$doc2Sidebar->parentNode->replaceChild($newNode, $doc2Sidebar);


echo '<pre>';
echo $doc2->saveXML();
echo '</pre>';

There are also other ways to work with handling these types of documents, without the need to work directly with DOMDocument, you can try using phpQuery or DomCrawler , these are alternatives that can be very attractive.

Scroll to Top