Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand problem in XML when creating a URL String

I am working with an XML feed that has, as one of it's nodes, a URL string similar to the following:

http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris

I understand that ampersands cause a lot of problems in XML and should be escaped by using & instead of a naked &. I therefore changed the php to read as follows:

<node><?php echo ('http://aflite.co.uk/track/?aid=13414&amp;mid=32532&amp;dl=http://www.google.com/&amp;aref=chris'); ?></node>

However when this generates the XML feed, the string appears with the full &amp; and so the actual URL does not work. Apologies if this is a very basic misunderstanding but some guidance would be great.

I've also tried using %26 instead of &amp; but still getting the same problem.

like image 393
Chris Avatar asked Dec 16 '22 12:12

Chris


1 Answers

If you are inserting something into XML/HTML you should always use the htmlspecialchars function. this will escape your strings into correct XML syntax.

but you are running into a second problem. your have added a second url to the first one. this need also escaped into url syntax. for this you need to use urlencode.

<node><?php echo htmlspecialchars('http://aflite.co.uk/track/?aid=13414&mid=32532&aref=chris&dl='.urlencode('http://www.google.com/')); ?></node>
like image 152
coding Bott Avatar answered Jan 05 '23 22:01

coding Bott