PHP DOM获取nodevalue html? (无剥离标签) [英] PHP DOM get nodevalue html? (without stripping tags)
问题描述
我正在尝试使用nodeValue获取文件中的div标签的innerhtml,但是这个代码只输出纯文本,并且似乎从div里面删除所有的html标签。如何更改此代码以输出div的HTML内容而不是纯文本,并输出主div包装它的子元素。
I am trying to get the innerhtml of div tags in a file using nodeValue, however this code is outputting only plain text and seems to strip out all html tag from inside the div. How can I change this code to output the div's HTML content and not plain text, AND also output the main div wrapping it's child elements.
示例:
file.txt的内容:
contents of file.txt:
<div class="1"><span class="test">text text text</span></div>
<div class="2"><span class="test">text text text</span></div>
<div class="3"><span class="test">text text text</span></div>
script.php:
script.php:
$file= file_get_contents('file.txt');
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">'.$file);
$entries = $doc->getElementsByTagName('div');
for ($i=0;$i<$entries->length;$i++) {
$entry = $entries->item($i);
echo $entry->nodeValue;
}
输出:文本文本texttext text texttext text text
outputs: text text texttext text texttext text text
我需要它输出:
<div class="1"><span class="test">text text text</span></div>
<div class="2"><span class="test">text text text</span></div>
<div class="3"><span class="test">text text text</span></div>
请注意,需要输出父div的(..etc)以及包装span标签。
Notice the parent div's (..etc) are needed to be outputted as well wrapping the span tags...
帮助!
推荐答案
我从来没有做过你尝试做,但是作为黑暗中的刺,使用API文档,echo $ entry-> textContent;工作?
I have never done what you're attempting to do, but as a stab in the dark, using the API docs, does echo $entry->textContent; work?
添加更新。这是来自 DOMNode 的文档页面上的评论:
Adding an update. This is from the comments located on the docs page for DOMNode:
嗨!
结合所有评论,获取节点内部HTML的最简单方法是使用此功能:
Combining all th comments, the easiest way to get inner HTML of the node is to use this function:
<?php function get_inner_html( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML; } ?>
或者,也许一个更简单的方法是做:
Or, maybe a simpler method is just to do:
echo $domDocument->saveXML($entry);
这篇关于PHP DOM获取nodevalue html? (无剥离标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!