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

Asked

Viewed 1,827 times

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;
  • 1

    You want to generate structured Html from another Html?

  • 1

    Try using html_entity_decode() within saveHTML().

  • Yes, I want to insert the contents of a div of a site into another html file, but this way the tags are treated as text

  • didn’t work out :/

  • You can try creating another Document for this piece of HTML and then import it into your DIV, like this: http://stackoverflow.com/questions/4400980/how-to-insert-html-to-php-domnode

1 answer

1

To resolve this issue, using only Domdocument, you must use the importNode, as 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 you passed. I just didn’t use foreach, because it already replaces direct, but if you want to go through with a repeat command, store Node in an array and then go through it again and the replace or add. You can remove we do not wish 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 forms of working with manipulation of these types of documents, without the need to work directly with the Domdocument, you can try using phpQuery or the Domcrawler, are alternatives that can be very attractive.

  • @brasofilo, (off-topic) my mistake. Actually it’s not quite plagiarism, but I should have put the references and citations. This will not be repeated.

  • @brasofilo you’re right. It was something unconscious, I got carried away at the time of answering. And I made this mistake even in many tags. Please reject or delete, the ones I send will put the source and excuse all this work. It will not repeat.

  • No problem, the mistake is who approved the issues, if they were rejected you would have realized. Anyway, nice job of filling in the empty tags ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.