简体   繁体   中英

Handling HTML entities in contenteditable divs

I am using a contenteditable <div> to edit an e-mail message (specifically, with NicEdit ). I want to be able to submit the message body in a form, process it with PHP, and send it using the mail() function (or PHPMailer ).

The problem I'm having is to do with special characters and encoding. When a special character such as π or á is typed into the div, it remains unchanged instead of being converted to an html entity like &pi; or &aacute; . There are some cases where it is converted, like < to &lt; .

So when the form is submitted, the data received by PHP is difficult to handle. Writing

pi is "less than" 2 × pi:
π < 2π

puts the following data in the contenteditable div

<div>pi is "less than" 2 × pi:</div>
<div>π < 2π</div>

and this is what is received by PHP. But using htmlentities() gives

&lt;div&gt;pi is &quot;less than&quot; 2 &times; pi:&lt;/div&gt;
&lt;div&gt;&pi; &lt; 2&pi;&lt;/div&gt;

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.

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:

<div>pi is &quot;less than&quot; 2 &times; pi:</div>
<div>&pi; &lt; 2&pi;</div>

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.

Any ideas?

EDIT: I have tried this solution 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 π . But the answer from that link has been voted down and a similar answer says the approach is fundamentally wrong. Can anyone tell me why this is, and why I shouldn't stick with htmlentitiesOutsideHTMLTags ?

Just an idea :

  • Step 1 : Convert your <div> <b></b> </div> to [div] [b] [/b] [/div]
  • Step 2 : You Htmlentities
  • Step 3 : Convert your [div] [b] [/b] [/div] to <div> <b> </b> </div>

By the way, I think that you can use strip_tags() before the step 1...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM