DOMDocument追加已经固定的字符串html

时间:2013-03-27 22:02:30

标签: php domdocument

所以我试图使用DOMDocument从页面显示快照html。到目前为止,我的主页是从远程位置加载的

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

然后我选择两个元素,我的头部区域和主要内容中的div,我将添加我的内容(html取自字符串变量)

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

在此之后,我已将内容添加到我的脚本标记

$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字符串中用html标签格式化。现在我的问题....每当我尝试将其添加到我的$ div节点内时,我无法将此内容视为HTML,因此我在显示此内容时遇到问题。

$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”形成良好,然后在这个家伙里面我有

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

当然,我想把所有这些标签都读成html!正确?

1 个答案:

答案 0 :(得分:3)

我认为您需要做的是创建一个文档片段:

$div = $dom->createElement( 'div'); // No $contents here
$fragment = $dom->createDocumentFragment();
$fragment->appendXML( $content);
$div->appendChild( $fragment);
相关问题