Remove &amp from string when writing to xml in PHP

As Gordon said, URIs are encoded this way. If you didn’t encode the & to a &, the XML file would be messed up – you’d get errors parsing it. When you take the string back out of the XML file, if the &amp still shows up, either str_replace() like this:

$str = str_replace('&', '&', $str)

Or use htmlspecialchars_decode():

$str = htmlspecialchars_decode($str);

The added bonus of using htmlspecialchars_decode() is that it will decode any other HTML that might be in the string. For more, see here.

Leave a Comment