2010-01-18 73 views

回答

118

Compare该更新的变种与PHP Manual User Note #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); 
} 
?> 
+0

谢谢。它工作正常。不应该$ dom-> preserveWhiteSpace = false;在文件加载之前? – JohnM2 2010-01-18 18:59:23

+0

@ JohnM2:[是的,应该](http://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way)。 – hakre 2013-06-23 18:35:31

+0

附加说明:自PHP 5.3.6开始,您可以省去临时的'DOMDocument'。还有人可能想用'ltrim'替换'trim'(甚至完全删除它)以保留一些空白,如分行符。 – hakre 2013-06-23 22:01:54

3
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); 
    } 
} 
6

要返回元素的html,您可以使用C14N()

$dom = new DOMDocument(); 
$dom->loadHtml($html); 
$x = new DOMXpath($dom); 
foreach($x->query('//table') as $table){ 
    echo $table->C14N(); 
} 
+2

C14N将尝试将HTML转换为有效的XML。例如
将变为

ajaybc 2016-05-18 04:05:54

+0

这是一种甩掉元素的HTML的肮脏方式,无需使用saveHTML来输出html,head和body标签。 – 2016-05-18 14:53:26

3

简化哈伊姆Evgi的回答版本:

<?php 

function innerHTML(\DOMElement $element) 
{ 
    $doc = $element->ownerDocument; 

    $html = ''; 

    foreach ($element->childNodes as $node) { 
     $html .= $doc->saveHTML($node); 
    } 

    return $html; 
} 

用法示例:

<?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> 
*/ 

有没有必要设置preserveWhiteSpaceformatOutput

14

这里是一个函数式编程风格版本:

function innerHTML($node) { 
    return implode(array_map([$node->ownerDocument,"saveHTML"], 
          iterator_to_array($node->childNodes))); 
} 
4

除了trincot的漂亮版本array_mapimplode但这次array_reduce

return array_reduce(
    iterator_to_array($node->childNodes), 
    function ($carry, \DOMNode $child) { 
     return $carry.$child->ownerDocument->saveHTML($child); 
    } 
); 

仍然不理解,为什么没有reduce()方法接受数组和迭代器。