Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling HTML entities in contenteditable divs
    text
    copied!<p>I am using a contenteditable <code>&lt;div&gt;</code> to edit an e-mail message (specifically, with <a href="http://nicedit.com/" rel="nofollow noreferrer">NicEdit</a>). I want to be able to submit the message body in a form, process it with PHP, and send it using the <code>mail()</code> function (or <a href="http://code.google.com/a/apache-extras.org/p/phpmailer/" rel="nofollow noreferrer">PHPMailer</a>).</p> <p>The problem I'm having is to do with special characters and encoding. When a special character such as <code>π</code> or <code>á</code> is typed into the div, it remains unchanged instead of being converted to an html entity like <code>&amp;pi;</code> or <code>&amp;aacute;</code>. There are some cases where it is converted, like <code>&lt;</code> to <code>&amp;lt;</code>.</p> <p>So when the form is submitted, the data received by PHP is difficult to handle. Writing</p> <pre><code>pi is "less than" 2 × pi: π &lt; 2π </code></pre> <p>puts the following data in the contenteditable div</p> <pre><code>&lt;div&gt;pi is "less than" 2 × pi:&lt;/div&gt; &lt;div&gt;π &lt; 2π&lt;/div&gt; </code></pre> <p>and this is what is received by PHP. But using <code>htmlentities()</code> gives</p> <pre><code>&amp;lt;div&amp;gt;pi is &amp;quot;less than&amp;quot; 2 &amp;times; pi:&amp;lt;/div&amp;gt; &amp;lt;div&amp;gt;&amp;pi; &amp;lt; 2&amp;pi;&amp;lt;/div&amp;gt; </code></pre> <p>which is correct except for the HTML tags, so this method appears to be useless. The originally received data is fine for inserting into a database (I think), but when I attempt to send it as an e-mail the encoding messes up.</p> <p>From my point of view it seems like the solution is to encode the entities that are outside of HTML tags to get something like this:</p> <pre><code>&lt;div&gt;pi is &amp;quot;less than&amp;quot; 2 &amp;times; pi:&lt;/div&gt; &lt;div&gt;&amp;pi; &amp;lt; 2&amp;pi;&lt;/div&gt; </code></pre> <p>but from searching on Google and StackOverflow this seems to be a bad thing to do. So I think I must be doing something wrong with the encoding at some point, whether that's just before sending the e-mail or back with the original contenteditable data. I'm looking for a solution that works, ideally without some complicated library like HTMLPurifier.</p> <p>Any ideas?</p> <p><strong>EDIT:</strong> I have tried <a href="https://stackoverflow.com/questions/1364933/htmlentities-in-php-but-preserving-html-tags/10277635#10277635">this solution</a> to convert special characters to html entities when they are not in tags. This seems to work well when I try typing in special characters like <code>π</code>. But the answer from that link has been voted down and <a href="https://stackoverflow.com/a/6332363/870965">a similar answer</a> says the approach is fundamentally wrong. Can anyone tell me why this is, and why I shouldn't stick with <code>htmlentitiesOutsideHTMLTags</code>? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload