title: How to wrap all text nodes with tags?
body: <p>How to wrap all text nodes with tags?</p>
  
  <p>Input:</p>
  
  <pre><code>&lt;p&gt;Before &lt;span&gt;text1 &lt;i&gt;text2&lt;/i&gt; text3&lt;/span&gt; after&lt;/p&gt;
  </code></pre>
  
  <p>Desirable output:</p>
  
  <pre><code>&lt;p&gt;&lt;b&gt;Before &lt;/b&gt;&lt;span&gt;&lt;b&gt;text1 &lt;/b&gt;&lt;i&gt;&lt;b&gt;text2&lt;/b&gt;&lt;/i&gt;&lt;b&gt; text3&lt;/b&gt;&lt;/span&gt;&lt;b&gt; after&lt;/b&gt;&lt;/p&gt;
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
  
  $html = &lt;&lt;&lt;'HTML'
  &lt;div&gt;
    &lt;p&gt;Before &lt;span&gt;text1 &lt;i&gt;text2&lt;/i&gt; text3&lt;/span&gt; after&lt;/p&gt;
  &lt;/div&gt;
  HTML;
  
  $dom = new DOMDocument;
  $dom-&gt;preserveWhiteSpace = false;
  $html = mb_convert_encoding( $html, "HTML-ENTITIES", "UTF-8" );
  @$dom-&gt;loadHTML( $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
  
  $xpath = new DOMXPath( $dom );
  
  $childrens = $xpath-&gt;query('//p[1]/descendant-or-self::node()');
  
  foreach ( $childrens as $child ) {
    $child-&gt;ownerDocument-&gt;saveHTML( $child );
  }
  
  echo $dom-&gt;saveHTML();
  </code></pre>
  
  <p>Thanks!</p>