2

So im trying to show a snapshot html from a page using DOMDocument. What i have so far is my home page which i load from a remote location

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($remote);

then i choose two elements, my head area and a div inside main content where i will be adding my content (html took from a string variable)

$head = $dom->getElementsByTagName('head')->item(0);
$noticia2 = $dom->getElementById('content_home');

following this i have added content to my script tag

$jquery = '$(document).ready(function(){ $("#feed_home").hide()});
      if (window.location.hash) {
        Destino = window.location.hash.replace(\'#!\', \'?\');

             window.location.href = window.location.href.split(\'#\')[0] + Destino;
    }


';    

$script = $dom->createElement('script', $jquery);
$script_type = $dom->createAttribute('type');

$script_type->value = 'application/javascript';
$script->appendChild($script_type);

$head->appendChild($script);

Then i go through my database and i collect some info which i will be formating with html tags in a big $content string using mysql_fetch_array. And now my problem.... whenever i try to append this inside my $div node i cant manage to have this content treated as HTML so im having problems displaying this stuff.

$div = $dom->createElement('div', $content);
$div_class = $dom->createAttribute('class');
$div_class->value = 'noticia-ajax';
$div->appendChild($div_class);
$noticia2->appendChild($div);
echo $dom->saveHTML();

What i get is a normal page, with my div "noticia-ajax" well formed and then inside this guy i have

SOME TEXT <BR> AND NO HTML TAG TREATED AS HTML TAG <BR> SOMETHING LIKE THIS 

And ofcourse i would like to have all of this tags read as html! right?

Fo Nko
  • 620
  • 10
  • 22
  • sorry! no... its a string with html ! NO ARRAY, my bad. – Fo Nko Mar 27 '13 at 22:09
  • See my answer to a similar question: http://stackoverflow.com/questions/2233683/php-domdocument-replace-domelement-child-with-html-string/17661043#17661043 – Codebeat Jul 15 '13 at 18:23

1 Answers1

4

I think what you need to do is create a document fragment:

$div = $dom->createElement( 'div'); // No $contents here
$fragment = $dom->createDocumentFragment();
$fragment->appendXML( $content);
$div->appendChild( $fragment);
nickb
  • 59,313
  • 13
  • 108
  • 143
  • why append XML instad of child? – Fo Nko Mar 27 '13 at 22:25
  • @FoNko - appendChild expects a DOMNode, you don't have one. All you have is a string of XML (in this case, HTML) data. – nickb Mar 27 '13 at 22:27
  • it says document fragment is empty :S for line $div->appendChild( $fragment); – Fo Nko Mar 27 '13 at 22:35
  • 1
    That `
    ` tag needs to be `
    `, when it is, [it works for me](http://3v4l.org/YPQFY).
    – nickb Mar 27 '13 at 22:44
  • lol... im pushing it to hard to my brain...i need to rest! THANKS SR! – Fo Nko Mar 27 '13 at 22:49
  • just as a note, I was trying something similar and had to add this to avoid malformed xml from taking down the site (the string I want to inject is provided by site administrators, who may not know the difference between xml and html): `libxml_use_internal_errors(true); if( simplexml_load_string( $output ) ) { ... }` The result is if it is malformed xml, it will not be added. – Jeff Lambert Jun 19 '13 at 13:04