title: PHP's domdocument inserts Â symbol
body: <p>Given some HTML, I apply the <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow noreferrer">http://php.net/manual/en/class.domdocument.php</a> class to it, save it, and the <code>Â</code> symbol is occasionally inserted.  It appears to happen with tags which have a single white-space (opposed to a <code>&amp;nbsp;</code>), but doesn't appear to be absolute (only the first <code>&lt;span&gt;</code> element exhibits this phenomena).  I've tried adding encoding when displaying the resulting HTML as recommended by <a href="https://stackoverflow.com/questions/13629351/php-domdocument-getelementbyid-adding-%C3%82-in-place-of-empty-span">PHP DOMDocument-&gt;getElementByID adding &#194; in place of empty &lt;span&gt;</a>, however, the problem persists.  What causes this and how is it prevented?</p>
  
  <p>In case you are interested why I am doing so.  I have an application where I am substituting HTML images with text.  When copying and pasting HTML from an Outlook email to a TinyMCE editor, and then parsing the HTML, I experience this behavior.</p>
  
  <pre><code>&lt;?php
  $message = &lt;&lt;&lt;EOT
  &lt;p&gt;Start&lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p class="MsoNormal"&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;"&gt;Phone: (444) 777-7777&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;End&lt;/p&gt;
  EOT;
      echo('&lt;p&gt;Initial HTML:&lt;/p&gt; '.$message);
      $message_encoded = utf8_encode($message);
      $doc = new DOMDocument();
      $doc-&gt;loadHTML($message);
      $body = $doc-&gt;getElementsByTagName('body')-&gt;item(0);
      $message=$doc-&gt;saveHTML($body);
      echo('&lt;p&gt;Final HTML:&lt;/p&gt; '.$message);
      echo('&lt;p&gt;Initial HTML encoded:&lt;/p&gt; '.$message_encoded);
      $doc-&gt;loadHTML($message_encoded);
      $body = $doc-&gt;getElementsByTagName('body')-&gt;item(0);
      $message_encoded=$doc-&gt;saveHTML($body);
      echo('&lt;p&gt;Final HTML:&lt;/p&gt; '.$message_encoded);
  ?&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;p&gt;Initial HTML:&lt;/p&gt; &lt;p&gt;Start&lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt; &lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p class="MsoNormal"&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;"&gt;Phone: (444) 777-7777&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;End&lt;/p&gt;&lt;p&gt;Final HTML:&lt;/p&gt; &lt;body&gt;
  &lt;p&gt;Start&lt;/p&gt;
  &lt;p&gt;Â &lt;/p&gt;
  &lt;p&gt;Â &lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p class="MsoNormal"&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt;Â &lt;br&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;"&gt;Phone: (444)Â 777-7777&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br&gt;&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;End&lt;/p&gt;
  &lt;/body&gt;&lt;p&gt;Initial HTML encoded:&lt;/p&gt; &lt;p&gt;Start&lt;/p&gt;
  &lt;p&gt;Â &lt;/p&gt;
  &lt;p&gt;Â &lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p class="MsoNormal"&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt;Â &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;"&gt;Phone: (444)Â 777-7777&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br /&gt;&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;End&lt;/p&gt;&lt;p&gt;Final HTML:&lt;/p&gt; &lt;body&gt;
  &lt;p&gt;Start&lt;/p&gt;
  &lt;p&gt;ÃÂ &lt;/p&gt;
  &lt;p&gt;ÃÂ &lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p class="MsoNormal"&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt;ÃÂ &lt;br&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br&gt;&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Arial, 'sans-serif';color:#000080;"&gt;Phone: (444)ÃÂ 777-7777&lt;/span&gt;
  &lt;span style="font-size:10pt;font-family:Calibri, 'sans-serif';color:#000080;"&gt; &lt;br&gt;&lt;/span&gt;
  &lt;/p&gt;
  &lt;p&gt;End&lt;/p&gt;
  &lt;/body&gt;
  </code></pre>