如何将文件内容导入节点

时间:2014-07-02 08:39:14

标签: php dom

我有许多文件包含各种HTML,文本或更多的PHP代码。我想从许多这些文件中获取所有内容,并将它们附加到各种元素中。

我几乎让它工作但内容被引号括起来:这是我的代码

//load the document containing the li tags i wish to append html too
dom = new DOMDocument;
$dom->loadHTML(file_get_contents('dashboard.html'));
$widgets = $dom->getElementsByTagName('li');

//count through each li and append html from its corresponding file
foreach($widgets as $widget){
    $id = $widget->getAttribute('id');
    $widget_content = new DOMText(file_get_contents('widgets/'.$id.'/'.$id.'.html'));
    $widget->appendChild($widget_content);
}

//echo results
<?php echo $dom->saveHTML() ?>

除了li内容用引号括起来之外,这个工作正常,因此html显示为纯文本。我尝试过使用DOMNode和DOMElement而不是DOMText,但是我收到了错误。

警告:DOMNode :: appendChild():无法获取DOMNode

编辑:

添加id来自

0 个答案:

没有答案
相关问题