简体   繁体   English

在可内容编辑的div中处理HTML实体

[英]Handling HTML entities in contenteditable divs

I am using a contenteditable <div> to edit an e-mail message (specifically, with NicEdit ). 我正在使用一个内容可编辑的<div>编辑电子邮件(特别是使用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 ). 我希望能够以某种形式提交消息正文,使用PHP处理它,并使用mail()函数(或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; 当在div中键入诸如πá这样的特殊字符时,它保持不变,而不是转换为&pi;这样的html实体&pi; or &aacute; &aacute; . There are some cases where it is converted, like < to &lt; 在某些情况下会对其进行转换,例如<&lt; .

So when the form is submitted, the data received by PHP is difficult to handle. 因此,提交表单时,PHP接收的数据很难处理。 Writing 写作

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

puts the following data in the contenteditable div 将以下数据放入contenteditable div中

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

and this is what is received by PHP. 这就是PHP所收到的。 But using htmlentities() gives 但是使用htmlentities()

&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. 除HTML标记外,这是正确的,因此此方法似乎无用。 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: 从我的角度来看,解决方案似乎是对HTML标签之外的实体进行编码,以得到如下所示的内容:

<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. 但是从Google和StackOverflow进行搜索看来,这似乎是一件坏事。 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. 我正在寻找一种可行的解决方案,理想情况下,它不需要像HTMLPurifier这样的复杂库。

Any ideas? 有任何想法吗?

EDIT: I have tried this solution to convert special characters to html entities when they are not in tags. 编辑:我尝试过此解决方案将特殊字符不在标记中时将其转换为html实体。 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 ? 谁能告诉我这是为什么,为什么我不应该坚持htmlentitiesOutsideHTMLTags

Just an idea : 只是一个想法:

  • Step 1 : Convert your <div> <b></b> </div> to [div] [b] [/b] [/div] 步骤1 :将您的<div> <b></b> </div>为[div] [b] [/ b] [/ div]
  • Step 2 : You Htmlentities 步骤2 :您的才能
  • Step 3 : Convert your [div] [b] [/b] [/div] to <div> <b> </b> </div> 第3步 :将您的[div] [b] [/ b] [/ div]转换为<div> <b> </b> </div>

By the way, I think that you can use strip_tags() before the step 1... 顺便说一句,我认为您可以在步骤1之前使用strip_tags()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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