DOMDocument追加字符串中已固定的html [英] DOMDocument append already fixed html from string
问题描述
因此,我尝试使用DOMDocument从页面显示快照html。到目前为止,我的主页是我从远程位置加载的
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);
然后我选择两个元素,即头部区域和主要内容中的div,我将在其中添加我的内容(来自字符串变量的html)
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);
然后我浏览数据库,收集一些信息,这些信息将使用使用mysql_fetch_array的大$ content字符串。现在是我的问题...。每当我尝试将其附加到$ div节点中时,我都无法将这些内容视为HTML,因此即时显示这些内容时遇到了问题。
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();
我得到的是一个普通页面,我的div noticia-ajax格式正确,然后在内部我有这个人
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
当然,我希望所有这些标签都读取为html!
And ofcourse i would like to have all of this tags read as html! right?
推荐答案
我认为您需要做的是创建文档片段:
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);
这篇关于DOMDocument追加字符串中已固定的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!