title: PHP: DOMDocument: Remove Unwanted Text from a Nested Element
body: <p>I have the following xml document:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;header level="2"&gt;My Header&lt;/header&gt;
  &lt;ul&gt;
      &lt;li&gt;Bulleted style text
          &lt;ul&gt;
              &lt;li&gt;
                  &lt;paragraph&gt;1.Sub Bulleted style text&lt;/paragraph&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul&gt;
      &lt;li&gt;Bulleted style text &lt;strong&gt;bold&lt;/strong&gt;
          &lt;ul&gt;
              &lt;li&gt;
                  &lt;paragraph&gt;2.Sub Bulleted &lt;strong&gt;bold&lt;/strong&gt;&lt;/paragraph&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>I need to remove the numbers preceeding the Sub-bulleted text.</strong> <em>1. and 2. in the given example</em></p>
  
  <p>This is the code I have so far:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  class MyDocumentImporter
  {
      const AWKWARD_BULLET_REGEX = '/(^[\s]?[\d]+[\.]{1})/i';
  
      protected $xml_string = '&lt;some_tag&gt;&lt;header level="2"&gt;My Header&lt;/header&gt;&lt;ul&gt;&lt;li&gt;Bulleted style text&lt;ul&gt;&lt;li&gt;&lt;paragraph&gt;1.Sub Bulleted style text&lt;/paragraph&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Bulleted style text &lt;strong&gt;bold&lt;/strong&gt;&lt;ul&gt;&lt;li&gt;&lt;paragraph&gt;2.Sub Bulleted &lt;strong&gt;bold&lt;/strong&gt;&lt;/paragraph&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/some_tag&gt;';
  
      protected $dom;
  
      public function processListsText( $loop = null ){
  
          $this-&gt;dom = new DomDocument('1.0', 'UTF-8');
  
          $this-&gt;dom-&gt;loadXML($this-&gt;xml_string);
  
          if(!$loop){
              //get all the li tags
              $li_set = $this-&gt;dom-&gt;getElementsByTagName('li');
          }
          else{
              $li_set = $loop;
          }
  
          foreach($li_set as $li){
  
              //check for child nodes
              if(! $li-&gt;hasChildNodes() ){
                  continue;
              }
  
              foreach($li-&gt;childNodes as $child){
                  if( $child-&gt;hasChildNodes() ){
                      //this li has children, maybe a &lt;strong&gt; tag
                      $this-&gt;processListsText( $child-&gt;childNodes );
                  }
                  if( ! ( $child instanceof DOMElement ) ){
                      continue;
                  }
                  if( ( $child-&gt;localName != 'paragraph') ||  ( $child instanceof DOMText )){
                      continue;
                  }
                  if( preg_match(self::AWKWARD_BULLET_REGEX, $child-&gt;textContent) == 0 ){
                      continue;
                  }
  
                  $clean_content = preg_replace(self::AWKWARD_BULLET_REGEX, '', $child-&gt;textContent);
  
                  //set node to empty
                  $child-&gt;nodeValue = '';
  
                  //add updated content to node
                  $child-&gt;appendChild($child-&gt;ownerDocument-&gt;createTextNode($clean_content));
  
                  //$xml_output = $child-&gt;parentNode-&gt;ownerDocument-&gt;saveXML($child);
                  //var_dump($xml_output);
  
              }
          }
      }
  }
  
  $importer = new MyDocumentImporter();
  $importer-&gt;processListsText();
  </code></pre>
  
  <p>The issue I can see is that <code>$child-&gt;textContent</code> returns the plain text content of the node, and strips the additional child tags. So:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;paragraph&gt;2.Sub Bulleted &lt;strong&gt;bold&lt;/strong&gt;&lt;/paragraph&gt;
  </code></pre>
  
  <p>becomes</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;paragraph&gt;Sub Bulleted bold&lt;/paragraph&gt;
  </code></pre>
  
  <p>The <code>&lt;strong&gt;</code> tag is no more.</p>
  
  <p>I'm a little stumped... Can anyone see a way to strip the unwanted characters, and retain the "inner child" <code>&lt;strong&gt;</code> tag?</p>
  
  <p>The tag may not always be <code>&lt;strong&gt;</code>, it could also be a hyperlink <code>&lt;a href="#"&gt;</code>, or <code>&lt;emphasize&gt;</code>.</p>