In php, Prepare string and create XML / RSS feed

I want to create my own RSS / XML feed. I am fetching data from the database for display but I keep getting invalid character errors. If the string contains ampersands or other strange characters, the XML will be invalid.

I've tried using urlencode and htmlentities, but they don't capture every possible character that needs to be escaped. Does anyone know of a PHP function that will prepare a string for XML output?

+2


a source to share


2 answers


htmlspecialchars should be enough. But don't forget to set the third parameter (charset) to the character set that matches the string encoding.



+1


a source


For instance:



function html_special_chars($str)
{
     return preg_replace(array('/&/', '/"/'), array('&', '"'), $str);
}

      

0


a source







All Articles