Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make user input safe for XML

What is the best practice for generating valid XML with PHP from user submitted text, e.g. eCommerce sales data with ampersands, angle brackets, non-ascii accent characters, new lines etc etc.

What functions, libraries, regexes do folks rely on?

like image 386
jerrygarciuh Avatar asked Jun 14 '12 19:06

jerrygarciuh


2 Answers

Wrap information in CDATA tags and encode data with htmlentities()

'<tag><![CDATA[' . htmlentities($theData) . ']]></tag>'

Or using DOM

$dom = new DOMDocument("1.0", "utf-8");

/* ... */

$dom->createCDATASection(htmlentities($theData));
like image 138
Steve Robbins Avatar answered Nov 02 '22 23:11

Steve Robbins


If you want binary safeness, then you need to use an additional transport encoding. For example you can use base64 or uuencode to store the data in a binary safe fashion inside an XML chunk.

like image 30
hakre Avatar answered Nov 02 '22 23:11

hakre