Html实体用php返回特殊字符
我想将特殊字符转换为 HTML 实体,然后再转换回原始字符。
我使用了htmlentities()
,然后使用了html_entity_decode()
。它对我来说效果很好,但是 {
和 }
不会返回到原始字符,它们仍然是 {
和 >}
。
我现在能做什么?
我的代码如下所示:
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
I want to convert special characters to HTML entities, and then back to the original characters.
I have used htmlentities()
and then html_entity_decode()
. It worked fine for me, but {
and }
do not come back to the original characters, they remain {
and }
.
What can I do now?
My code looks like:
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管没有人可以复制您的问题,但这里有一个简单的 str_replace 来解决它的直接方法。
演示(点击右上角的“源”链接)
编辑: 我现在看到问题了。如果您的输入字符串是:
对
htmlentities
的调用将&
编码为&
,这会为您提供字符串The
&
然后被解码回&
,输出:解决方法是再次通过
html_entity_decode
发送字符串,这将正确解码你的实体。Even though nobody can replicate your problem, here's a direct way to solve it by a simple str_replace.
Demo (click the 'Source' link in the top right)
Edit: I see the problem now. If your input string is:
A call to
htmlentities
encodes the&
into&
, which gives you the stringThe
&
is then later decoded back into&
, to output this:The fix is to send the string through
html_entity_decode
again, which will properly decode your entities.