[英]Html entities back to special characters with php
I want to convert special characters to HTML entities, and then back to the original characters. 我想将特殊字符转换为HTML实体,然后返回原始字符。
I have used htmlentities()
and then html_entity_decode()
. 我使用了htmlentities()
然后使用了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);
Even though nobody can replicate your problem, here's a direct way to solve it by a simple str_replace . 即使没有人可以复制你的问题,这是通过简单的str_replace解决它的直接方法。
$input = '<p><script type="text/javascript"> $(document).ready(function() { $("#left_side_custom_image").click(function() { alert("HELLO"); }); }); </script></p> ';
$output = str_replace( array( '{', '}'), array( '{', '}'), $input);
Demo (click the 'Source' link in the top right) 演示 (点击右上角的“来源”链接)
Edit: I see the problem now. 编辑:我现在看到了问题。 If your input string is: 如果您的输入字符串是:
"{hello}"
A call to htmlentities
encodes the &
into &
所述的呼叫htmlentities
编码&
成&
, which gives you the string ,它给你字符串
"&#123;hello}"
The &
&
is then later decoded back into &
, to output this: 然后被解码回&
,输出:
"{hello}"
The fix is to send the string through html_entity_decode
again, which will properly decode your entities. 修复方法是再次通过html_entity_decode
发送字符串,这将正确解码您的实体。
$custom_header = "{hello}";
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
echo html_entity_decode( $custom_header); // Outputs {hello}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.