title: Prevent HTMLPurifier and DOMDocument from converting &nbsp; to Â
body: <p>I am using TinyMCE to generate some HTML.  Note that TinyMCE creates line spaces using <code>&lt;p&gt;&amp;nbsp;&lt;/p&gt;</code>.</p>
  
  <p>I am then purifying the HTML using HTMLPurifier.  Note that the line spaces are converted to <code>&lt;p&gt; &lt;/p&gt;</code>.</p>
  
  <p>I then perform some DOM manipulation using PHP's DOMDocument.  I wish to purify the HTML perform DOM manipulation, and will be doing more modifications, but just am showing the portion below to demonstrator the problem.  Note that line spaces are converted to <code>&lt;p&gt;Â &lt;/p&gt;</code>.</p>
  
  <p>Why is this happening?</p>
  
  <p>Two solutions I found are stripping <code>Â</code> after performing the DOM manipulation and replacing <code>&amp;nbsp</code> with <code>@nbsp</code> before purifying and then replacing back after purifying.  What is the best way to prevent this from happening?</p>
  
  <pre><code>//HTML created using TinyMCE and sent to server using jQuery Ajax Post
  $message=trim($_POST['message']);
  syslog(LOG_INFO,$message);
  // Jul 28 09:39:24 devserver httpd: &lt;p&gt;Line 1&lt;/p&gt;#012&lt;p&gt;&amp;nbsp;&lt;/p&gt;#012&lt;p&gt;Line 2&lt;/p&gt;
  
  $config = HTMLPurifier_Config::createDefault();
  $config-&gt;set('Cache.SerializerPath',VS_TEMPORARY_SITE);
  $purifier = new HTMLPurifier($config);
  $message=$purifier-&gt;purify($message);
  syslog(LOG_INFO,$message);
  //Jul 28 09:39:24 devserver httpd: &lt;p&gt;Line 1&lt;/p&gt;#012&lt;p&gt; &lt;/p&gt;#012&lt;p&gt;Line 2&lt;/p&gt;
  
  $doc = new DOMDocument();
  $doc-&gt;loadHTML($message);
  $body = $doc-&gt;getElementsByTagName('body')-&gt;item(0);
  $message=$doc-&gt;saveHTML($body);    //Select the body tag
  $message = str_replace(array('&lt;body&gt;', '&lt;/body&gt;'), array('', ''), $message);  //strip the body tags
  syslog(LOG_INFO,$message);
  //Jul 28 09:39:24 devserver httpd: #012&lt;p&gt;Line 1&lt;/p&gt;#012&lt;p&gt;Â &lt;/p&gt;#012&lt;p&gt;Line 2&lt;/p&gt;
  </code></pre>