Robert Johnstone
Robert Johnstone

Reputation: 5371

php > SimpleXMLElement > '&' Symbol

I am having problems using the & in xml strings created by php's SimpleXMLElement. For example the following:

<?php
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><links></links>");
$xml->addChild("url","https://www.somewhere.com?a=1&b=2");
echo $xml->asXML();
?>

gives me when run:

<?xml version="1.0" encoding="UTF-8"?>
<links><url>https://www.somewhere.com?a=1</url></links>

I have already tried:

...
$xml->addChild("url","https://www.somewhere.com?a=1&amp;b=2");
...

and got:

...
<links><url>https://www.somewhere.com?a=1&amp;b=2</url></links>

how do I get:

...
<links><url>https://www.somewhere.com?a=1&b=2</url></links>

Upvotes: 1

Views: 2035

Answers (1)

lonesomeday
lonesomeday

Reputation: 237995

You can't. That is illegal XML: the & character must be escaped (XML specification).

Upvotes: 4

Related Questions