您使用什么函数在PHP DOM实现中获取给定DOMNode的innerHTML?有人可以提供可靠的解决方案吗?
当然,outerHTML也可以。
比较与此更新变种PHP手册用户注意#89718:
<?php function DOMinnerHTML(DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; } ?>
例:
<?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); } ?>