PHP DOMDocument table, but keep internal HTML content

I'm trying to display every row in an HTML table and leave the inner HTML tags in the results, but I can't get it to work and it keeps stripping the inner HTML tags, which is better for me to get every row from the HTML table and leave the inner HTML tags in results?

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

<?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();

      

The following is currently displayed, which is correct, but it is missing internal 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, 

      

Perhaps I am looking at this the wrong way, and there must be another way for me to extract information from the table to get the correct result. Any help would be appreciated.

+3


source to share


1 answer


DOMNode->nodeValue

is equivalent DOMNode->textContent

to DOMElement

(which is derived from DOMNode

); it will give you only text content and all its child nodes.

If you want 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;
}

      

+2


source







All Articles