When I use php to generate xml documents
I used this function from
http://us2.php.net/manual/en/function.htmlentities.php
function xml_character_encode($string, $trans='') {
$trans = (is_array($trans)) ? $trans : get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";
return strtr($string, $trans);
}
To convert the value from database to use in xml document.
But it still leaves some strange characters in my xml document.
So I use the following funcition.
function convert_smart_quotes($string)
{
$search = array(
'&',
'<',
'>',
'"',
chr(212),
chr(213),
chr(210),
chr(211),
chr(209),
chr(208),
chr(201),
chr(145),
chr(146),
chr(147),
chr(148),
chr(151),
chr(150),
chr(133)
);
$replace = array(
'&',
'<',
'>',
'"',
'‘',
'’',
'“',
'”',
'–',
'—',
'…',
'‘',
'’',
'“',
'”',
'–',
'—',
'…'
);
return str_replace($search, $replace, $string);
}
That seems taking care of the xml. But I am not sure if this function covers all
I would like to know is there a "near to official" xmlentities() function like htmlentities(), thanks!
I also found this
http://www.sourcerally.net/Scripts/39-Convert-HTML-Entities-to-XML-Entities