简体   繁体   English

将html实体转换为html字符的最佳方法

[英]Best way to convert html entities to html characters

What I need to do is convert any html entities to the actual html character. 我需要做的是将任何html实体转换为实际的html字符。 It can be in javascript or preferable php. 它可以使用javascript或首选php。

Here's the senario. 这是参议员。 When I save content to the database from TinyMCE it converts it to html entities for me. 当我从TinyMCE将内容保存到数据库时,它将为我将其转换为html实体。 That's all good and great when the webpage is rendered, but when I also what to put it in a text input the html entities stick and to a normal user it looks a little wonky. 呈现网页时,这一切都很好,也很不错,但是当我还要在文本输入中添加内容时,html实体就会粘住,对于普通用户而言,它看起来有点怪。

The only way I can think of doing it is have an array with the entity as the key and character as the value. 我能想到的唯一方法是将实体作为键,将字符作为值。 When an entity is found, check that array and spit out the value. 找到实体后,检查该数组并吐出值。

Does anyone have a better/more efficient solution, possibly using regex? 是否有人可能使用正则表达式有更好/更有效的解决方案?

html_entity_decode sounds like it is what you are after. html_entity_decode听起来就是您所追求的。 It isn't often that people want to take HTML input from users and then display it as text later though. 人们通常并不希望从用户那里获取HTML输入,然后稍后再将其显示为文本。

您显然可以在PHP级别使用html_entity_decode ,或者如果要通过ajax将数据发送到javascript,则可以使用unescape() javascript函数在客户端执行相同的操作

使用php函数html_entity_decode

Have you tried applying html_entity_decode to your text? 您是否尝试过将html_entity_decode应用于文本? Documentation here 这里的文件

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

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