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

4

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();

But 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;
    
asked by anonymous 25.07.2014 / 21:52

1 answer

1

To resolve this issue, using DOMDocument only, you must use the importNode , because it is two different DOMDocument the link of this documentation has the solution to your problem, below is the example of it already implemented with the code that you passed. I just did not use the foreach, because it already replaces it directly, but if you want, you can go through a repeat command, store the node in an array and then re-run it and replace or add . You can remove nodes that you do not 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 of working with manipulation of these types of   documents, without the need to work directly with the   DOMDocument, you can try using phpQuery or    DomCrawler are alternatives that can be very attractive.

    
29.07.2014 / 02:15