简体   繁体   中英

PHP DOMDocument table but leave inner HTML content

I'm trying to display each row in a HTML table and leave the inner HTML tags in the results but I can't get it to work and it keeps stripping the HTML inner tags what is the best for me to get each row from a HTML table and leave the inner HTML tags in the results?

This is the code I'm currently working with to get this working:

<?php
 function tdrows($elements){
   $str = "";
   foreach ($elements as $element) {
     $str .= $element->nodeValue . ", ";
   }

  return $str;
 }

 function getdata(){
   $content = "<table border="0" cellspacing="0" cellpadding="0" class="ta1"><colgroup><col width="99"/><col width="99"/><col width="99"/><col width="99"/></colgroup><tr class="ro1"><td style="text-align:left;width:2.267cm; " class="ce1"><p>Col A</p></td><td style="text-align:left;width:2.267cm; " class="ce2"><p>Col B</p></td><td style="text-align:left;width:2.267cm; " class="ce3"><p>Col C</p></td><td style="text-align:left;width:2.267cm; " class="ce5"><p>Col D</p></td></tr><tr class="ro2"><td style="text-align:left;width:2.267cm; " class="Default"><p>This <span class="T1">is</span> a test</p></td><td style="text-align:left;width:2.267cm; " class="Default"><p>This is a <span class="T2">test</span></p></td><td style="text-align:left;width:2.267cm; " class="ce4"><p>This<span class="T3"> is</span> a test<span class="T4">2</span></p></td><td style="text-align:left;width:2.267cm; " class="Default"><p>This is a test</p></td></tr></table>";

   $DOM = new DOMDocument;
   $DOM->loadHTML($contents);

   $items = $DOM->getElementsByTagName('tr');

   foreach ($items as $node) {
     echo tdrows($node->childNodes) . "<br />";
   }
}

getdata();

It's currently display in the following what is correct but it's missing the inner HTML tags.

Col A, Col B, Col C, Col D, 
This is a test, This is a test, This is a test2, This is a test, 

Maybe I'm looking at it wrong and there should be another way for me to extract the information from a table to get the correct result. Any help would be grateful.

DOMNode->nodeValue is equivalent to DOMNode->textContent for DOMElement (which is a derivative of DOMNode ); it will only give you the text content of itself and all its descendant nodes.

If you want the HTML as well, you should use DOMDocument::saveHTML( DOMNode $node = null ) , with something like this:

function tdrows($elements){
  $str = "";
  foreach ($elements as $element) {
    $str .= $element->ownerDocument->saveHTML( $element );
  }

  return $str;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM