【问题标题】:DOMDocument append already fixed html from stringDOMDocument 从字符串追加已修复的 html
【发布时间】:2013-03-18 05:08:35
【问题描述】:

所以我试图从使用 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!对吧?

【问题讨论】:

标签: php domdocument


【解决方案1】:

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

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

【讨论】:

  • 为什么要附加 XML instad of child?
  • @FoNko - appendChild 需要一个 DOMNode,你没有。您所拥有的只是一串 XML(在本例中为 HTML)数据。
  • 它说文档片段是空的:S for line $div->appendChild($fragment);
  • 那个&lt;br&gt;标签必须是&lt;br /&gt;,如果是it works for me
  • 大声笑......我把它推到我的大脑里......我需要休息!谢谢先生!
猜你喜欢
  • 1970-01-01
  • 2015-02-15
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 2011-08-24
相关资源
最近更新 更多