title: PHP DOM - stripping span tags, leaving their contents
body: <p>I am looking to take markup like:</p>
  
  <pre><code>&lt;span class="test"&gt;Some text that is &lt;strong&gt;bolded&lt;/strong&gt; and contains a &lt;a href="#"&gt;link&lt;/a&gt;.&lt;/span&gt;
  </code></pre>
  
  <p>and find the best method in PHP for stripping the span so that what is left is this:</p>
  
  <pre><code>Some text that is &lt;strong&gt;bolded&lt;/strong&gt; and contains a &lt;a href="#"&gt;link&lt;/a&gt;.
  </code></pre>
  
  <p>I have read many of the other questions regarding parsing HTML using PHP DOM instead of regex, but have been unable to figure out a way to strip the spans with PHP DOM, leaving the HTML contents intact. The ultimate goal is to be able to strip the document of all span tags, leaving their contents. Can this be done with PHP DOM? Is there a method that provides better performance and does not rely on string parsing instead of DOM parsing?</p>
  
  <p>I've used regex to do so, without any issues thus far:</p>
  
  <pre><code>/&lt;(\/)?(span)[^&gt;]*&gt;/i
  </code></pre>
  
  <p>But my interest here is in becoming a better PHP programmer. And since it is always possible to trip up a regex with badly formatted markup, I'm looking for a better way. I have also considered using strip_tags(), doing something like the following:</p>
  
  <pre><code>public function strip_tags( $content, $tags_to_strip = array() )
  {
      // All Valid XHTML tags
   $valid_tags = array(
    'a','abbr','acronym','address','area','b','base','bdo','big','blockquote','body','br','button','caption','cite',
    'code','col','colgroup','dd','del','dfn','div','dl','DOCTYPE','dt','em','fieldset','form','h1','h2','h3','h4',
    'h5','h6','head','html','hr','i','img','input','ins','kbd','label','legend','li','link','map','meta','noscript',
    'object','ol','optgroup','option','p','param','pre','q','samp','script','select','small','span','strong','style',
    'sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','ul','var'
   );
  
      // Remove each tag to strip from the valid_tags array
   foreach ( $tags_to_strip as $tag ){
    $ndx = array_search( $tag, $valid_tags );
    if ( $ndx !== false ){
     unset( $valid_tags[ $ndx ] );
    }
   }
  
      // convert valid_tags array into param for strip_tags
   $valid_tags = implode( '&gt;&lt;', $valid_tags );
   $valid_tags = "&lt;$valid_tags&gt;";
  
   $content = strip_tags( $content, $valid_tags );
   return $content;
  }
  </code></pre>
  
  <p>But this is still parsing the string, and not DOM parsing. So if the text is mal-formed, it is possible to strip too much. Many people are quick to suggest using <a href="http://simplehtmldom.sourceforge.net/" rel="nofollow">Simple HTML DOM Parser</a>, but looking at the source code, it seems to be using regex to parse the html as well.</p>
  
  <p>Can this be done with PHP5's DOM, or is there a better way to strip tags leaving their contents intact. Would it be bad practice to use Tidy or <a href="http://htmlpurifier.org/" rel="nofollow">HTML Purifier</a> to clean the text and then use regex / HTML Simple HTML DOM parser on it?</p>
  
  <p>Libraries like <a href="http://code.google.com/p/phpquery/" rel="nofollow">phpQuery</a> seem to be too heavy weight for what seems like it should be a simple task.</p>