Decoding XML String Values (PHP version)

I have already explained how to encode values in an XML string (using Javascript) here. So, if you have encoded the values in your XML strings using my javascript function then you can use the PHP function(explained below) to decode the values in those XML string on the server side (i.e. backend PHP side.)

function xmldecode($txt)
    $txt = str_replace('&',		'&',	$txt);
    $txt = str_replace('&lt;',		'<',	$txt);
    $txt = str_replace('&gt;',		'>',	$txt);
    $txt = str_replace('&apos;',	"'",	$txt);
    $txt = str_replace('&quot;', 	'"',	$txt);
    return $txt;

Using xmldecode() - A simple example (PHP Code):

So, you can use the function (above) as follows:

$xml  = new SimpleXMLElement($xmlstr);
$name_from_js = xmldecode(trim($xml->name));

Please note that SimpleXMLElement is a PHP function that helps parse XML strings.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.

tags cloud icon tags cloud


popular searches icon popular searches