1

Found my answer here by using saveHtml of the particular node.


I am trying to convert a few spans into an unordered list. I have to assume that the list items are separated by a <span ... </span>

For example, this is some of the html that I need to convert:

<span class="FirstClass"> Send the author a PM </span>
<span id="blah"><a href="a_link_here"</a></span>
<span><img src="www...." /></span>

I can replicate these into a list, but I don't know how to preserve the hyperlinks and images in each span. I can only get the nodeValue and Attributes. I really just need to get everything in between.

This is what I am doing:

$elements = $doc->getElementsByTagName('span');
$i = 0;
echo '<ul>';
 foreach ($elements as $param) {
  $node = $elements->item($i);
  echo '<li>';
   if ($node->hasAttributes()) {
        echo '<span ';
        foreach ($node->attributes as $attr) {
        echo $attr->nodeName.'="'.$attr->nodeValue.'"';
   }
     echo '>';
      }
     else
        echo '<span>';
     echo  $node->nodeValue . '</span></li>';
     $i++;
  }
   echo '</ul>';
Community
  • 1
  • 1
Justin
  • 183
  • 1
  • 1
  • 11

0 Answers0