How to escape &
in a URL inside an XML element.
I have a URL inside a JavaScript object, I pass the object to PHP where the following code turns it into XML, then I put it in an XML file:
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<items id="datasource"/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $newChild = $xml->addChild('item'));
$newChild->addAttribute('id', $key);
}
else {
$newChild = $xml->addChild('item', $value);
$newChild->addAttribute('id', $key);
}
}
return $xml->asXML();
}
The URL needs 2 query parameters so I need the &
, but for some reason I can't escape it wihout allowing me to open the link.
I have tried replacing &
with: &
, &
, %26
and <![CDATA[&]]>
but none of these work.