php 如何获取DOMNode的innerHTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2087103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get innerHTML of DOMNode?
提问by Dawid Ohia
What function do you use to get innerHTML of a given DOMNode in the PHP DOM implementation? Can someone give reliable solution?
在 PHP DOM 实现中,您使用什么函数来获取给定 DOMNode 的 innerHTML?有人可以提供可靠的解决方案吗?
Of course outerHTML will do too.
当然,outerHTML 也可以。
回答by Haim Evgi
Comparethis updated variant with PHP Manual User Note #89718:
比较与此更新变种PHP手册用户注意#89718:
<?php
function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
?>
Example:
例子:
<?php
$dom= new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($html_string);
$domTables = $dom->getElementsByTagName("table");
// Iterate over DOMNodeList (Implements Traversable)
foreach ($domTables as $table)
{
echo DOMinnerHTML($table);
}
?>
回答by trincot
Here is a version in a functional programmingstyle:
这是一个函数式编程风格的版本:
function innerHTML($node) {
return implode(array_map([$node->ownerDocument,"saveHTML"],
iterator_to_array($node->childNodes)));
}
回答by CONvid19
回答by Alf Eaton
A simplified version of Haim Evgi's answer:
Haim Evgi 回答的简化版:
<?php
function innerHTML(\DOMElement $element)
{
$doc = $element->ownerDocument;
$html = '';
foreach ($element->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
}
Example usage:
用法示例:
<?php
$doc = new \DOMDocument();
$doc->loadHTML("<body><div id='foo'><p>This is <b>an <i>example</i></b> paragraph<br>\n\ncontaining newlines.</p><p>This is another paragraph.</p></div></body>");
print innerHTML($doc->getElementById('foo'));
/*
<p>This is <b>an <i>example</i></b> paragraph<br>
containing newlines.</p>
<p>This is another paragraph.</p>
*/
There's no need to set preserveWhiteSpaceor formatOutput.
无需设置preserveWhiteSpace或formatOutput。
回答by flu
In addition to trincot's nice version with array_mapand implodebut this time with array_reduce:
除了 trincot 的漂亮版本和array_map,implode但这次是array_reduce:
return array_reduce(
iterator_to_array($node->childNodes),
function ($carry, \DOMNode $child) {
return $carry.$child->ownerDocument->saveHTML($child);
}
);
Still don't understand, why there's no reduce()method which accepts arrays and iterators alike.
仍然不明白,为什么没有reduce()接受数组和迭代器的方法。
回答by Chris
function setnodevalue($doc, $node, $newvalue){
while($node->childNodes->length> 0){
$node->removeChild($node->firstChild);
}
$fragment= $doc->createDocumentFragment();
$fragment->preserveWhiteSpace= false;
if(!empty($newvalue)){
$fragment->appendXML(trim($newvalue));
$nod= $doc->importNode($fragment, true);
$node->appendChild($nod);
}
}
回答by birgire
Here's another approach based on this commentby Drupella on php.net, that worked well for my project. It defines the innerHTML()by creating a new DOMDocument, importing and appending to it the target node, instead of explicitly iterating over child nodes.
这是基于Drupella 在 php.net上的评论的另一种方法,它适用于我的项目。它innerHTML()通过创建一个 new DOMDocument,导入并附加目标节点来定义 ,而不是显式迭代子节点。
InnerHTML
内部HTML
Let's define this helper function:
让我们定义这个辅助函数:
function innerHTML( \DOMNode $n, $include_target_tag = true ) {
$doc = new \DOMDocument();
$doc->appendChild( $doc->importNode( $n, true ) );
$html = trim( $doc->saveHTML() );
if ( $include_target_tag ) {
return $html;
}
return preg_replace( '@^<' . $n->nodeName .'[^>]*>|</'. $n->nodeName .'>$@', '', $html );
}
where we can include/exclude the outer target tag through the second input argument.
我们可以通过第二个输入参数包含/排除外部目标标签。
Usage Example
使用示例
Here we extract the inner HTML for a target tag given by the "first" id attribute:
在这里,我们为“first” id 属性给出的目标标签提取内部 HTML:
$html = '<div id="first"><h1>Hello</h1></div><div id="second"><p>World!</p></div>';
$doc = new \DOMDocument();
$doc->loadHTML( $html );
$node = $doc->getElementById( 'first' );
if ( $node instanceof \DOMNode ) {
echo innerHTML( $node, true );
// Output: <div id="first"><h1>Hello</h1></div>
echo innerHTML( $node, false );
// Output: <h1>Hello</h1>
}
Live example:
现场示例:
http://sandbox.onlinephpfunctions.com/code/2714ea116aad9957c3c437d46134a1688e9133b8
http://sandbox.onlinephpfunctions.com/code/2714ea116aad9957c3c437d46134a1688e9133b8
回答by Marco Marsala
Old query, but there is a built-in method to do that. Just pass the target node to DomDocument->saveHtml().
旧查询,但有一个内置方法可以做到这一点。只需将目标节点传递给DomDocument->saveHtml().
Full example:
完整示例:
$html = '<div><p>ciao questa è una <b>prova</b>.</p></div>';
$dom = new DomDocument($html);
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$node = $xpath->query('.//div/*'); // with * you get inner html without surrounding div tag; without * you get inner html with surrounding div tag
$innerHtml = $dom->saveHtml($node);
var_dump($innerHtml);
Output: <p>ciao questa è una <b>prova</b>.</p>
输出: <p>ciao questa è una <b>prova</b>.</p>

