Danish Ahmed Danish Ahmed - 7 months ago 25
PHP Question

php convert special character '&' to single character

I have a string containing character

&


And i have got the same string from
$_POST
containing
&
. when i var_dump it, the string form post shows 4 more characters then the
var_dump(_same_string_)
.

I want to treat special characters from "post" as a "single character". What should I do?

I tried:

$postString = (string)$postString;


tried:

vardump(htmlspecialchars($postString));


it shows
&
as
&amp
while in
var_dump(_same_string_)
&
remains same.

Answer

To convert from html entities back to the characters. Like from &amp to &, and &gt to >. Use htmlspecialchars_decode().

To do it the other way around, like & to &amp, use htmlspecialchars().