title: Manipulate the content of HTML strings without changing the HTML
body: <p>If I have a string of HTML, maybe like this...</p>
  
  <pre><code>&lt;h2&gt;Header&lt;/h2&gt;&lt;p&gt;all the &lt;span class="bright"&gt;content&lt;/span&gt; here&lt;/p&gt;
  </code></pre>
  
  <p>And I want to manipulate the string so that all words are reversed for example...</p>
  
  <pre><code>&lt;h2&gt;redaeH&lt;/h2&gt;&lt;p&gt;lla eht &lt;span class="bright"&gt;tnetnoc&lt;/span&gt; ereh&lt;/p&gt;
  </code></pre>
  
  <p>I know how to extract the string from the HTML and manipulate it by passing to a function and getting a modified result, but how would I do so whilst retaining the HTML?</p>
  
  <p>I would prefer a non-language specific solution, but it would be useful to know php/javascript if it must be language specific.</p>
  
  <h2>Edit</h2>
  
  <p>I also want to be able to manipulate text that spans several DOM elements...</p>
  
  <pre><code>Quick&lt;em&gt;Draw&lt;/em&gt;McGraw
  
  warGcM&lt;em&gt;warD&lt;/em&gt;kciuQ
  </code></pre>
  
  <h2>Another Edit</h2>
  
  <p>Currently, I am thinking to somehow replace all HTML nodes with a unique token, whilst storing the originals in an array, then doing a manipulation which ignores the token, and then replacing the tokens with the values from the array.</p>
  
  <p>This approach seems overly complicated, and I am not sure how to replace all the HTML without using REGEX which I have learned you can go to the stack overflow prison island for.</p>
  
  <h2>Yet Another Edit</h2>
  
  <p>I want to clarify an issue here. I want the text manipulation to happen over <code>x</code> number of DOM elements - so for example, if my formula randomly moves letters in the middle of a word, leaving the start and end the same, I want to be able to do this...</p>
  
  <pre><code>&lt;em&gt;going&lt;/em&gt;&lt;i&gt;home&lt;/i&gt;
  </code></pre>
  
  <p>Converts to</p>
  
  <pre><code>&lt;em&gt;goonh&lt;/em&gt;&lt;i&gt;gmie&lt;/i&gt;
  </code></pre>
  
  <p>So the HTML elements remain untouched, but the string content inside is manipulated (as a whole - so <code>goinghome</code> is passed to the manipulation formula in this example) in any way chosen by the manipulation formula.</p>