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);
Even though nobody can replicate your problem, here's a direct way to solve it by a simple 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 &
, 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.
$custom_header = "{hello}";
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
echo html_entity_decode( $custom_header); // Outputs {hello}