[ create a new paste ] login | about

Link: http://codepad.org/FulWwCbC    [ raw code | output | fork ]

PHP, pasted on Apr 3:
<?php

$str = <<<EOF
<code class="php"> <div> a div.. </div></code></code>

<pre class="php">
<div> a div.. </div>
</pre>

<div> this should be ignored </div>
EOF;

function recurse(&$doc, &$parent) {
   if (!$parent->hasChildNodes())
      return;
   
   foreach ($parent->childNodes as $elm) {
      
      if ($elm->nodeName == "code" || $elm->nodeName == "pre") {
         $content = '';
         while ($elm->hasChildNodes()) {
             $child = $elm->childNodes->item(0);
             $content .= htmlspecialchars($doc->saveXML($child));
             $elm->removeChild($child);
         }
         $elm->appendChild($doc->createTextNode($content));
      }
      else {
         recurse($doc, $elm);
      }
   }
}

$doc = new DOMDocument();
$doc->loadXML("<document>" . $str . "</document>");

// Iterate the DOM, finding <code /> and <pre /> tags:
recurse($doc, $doc->documentElement);
foreach ($doc->childNodes->item(0)->childNodes as $node) {
   echo $doc->saveXML($node);
}

?>


Output:
1
2
3
4
5
6

Warning: DOMDocument::loadXML(): Opening and ending tag mismatch: document line 1 and code in Entity, line: 1 on line 35

Warning: DOMDocument::loadXML(): Extra content at the end of the document in Entity, line: 3 on line 35

Fatal error: Call to a member function hasChildNodes() on a non-object on line 14


Create a new paste based on this one


Comments: