In php, Prepare string and create XML/RSS Feed

送分小仙女□ 提交于 2019-12-13 16:32:55

问题


I want to create my own RSS/XML feed. I fetch data from the database to display, but keep getting invalid character errors. If the string has an ampersand or other strange characters in it, the XML will be invalid.

I tried using urlencode and htmlentities, but these don't capture all possible characters which need to be escaped. Does anyone know of a PHP function which will prepare a string for XML output?


回答1:


htmlspecialchars should be enough. But don't forget to set the 3rd parameter (charset) to the character set matching the string charset.




回答2:


For example:

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


来源:https://stackoverflow.com/questions/2802695/in-php-prepare-string-and-create-xml-rss-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!