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?

2
  • sorry! no... its a string with html ! NO ARRAY, my bad. Commented Mar 27, 2013 at 22:09
  • See my answer to a similar question: stackoverflow.com/questions/2233683/… Commented Jul 15, 2013 at 18:23

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

why append XML instad of child?
@FoNko - appendChild expects a DOMNode, you don't have one. All you have is a string of XML (in this case, HTML) data.
it says document fragment is empty :S for line $div->appendChild( $fragment);
That <br> tag needs to be <br />, when it is, it works for me.
lol... im pushing it to hard to my brain...i need to rest! THANKS SR!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.