Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

php - DOMDocument append already fixed html from string

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...