I use this function to encode all the xml entities and also all the &something; that are not defined in xml like ™
You can also decode what you encode with my decode function.
My function works a little like the htmlentities.
You can also add other string to the array if you want to exclude them from the encoding.
<?php
function xml_entity_decode($text, $charset = 'Windows-1252'){
$text = html_entity_decode($text, ENT_COMPAT, $charset);
$text = html_entity_decode($text, ENT_COMPAT, $charset);
return $text;
}
function xml_entities($text, $charset = 'Windows-1252'){
$text = htmlentities($text, ENT_COMPAT, $charset, false);
$arr_xml_special_char = array(""","&","'","<",">");
$arr_xml_special_char_regex = "(?";
foreach($arr_xml_special_char as $key => $value){
$arr_xml_special_char_regex .= "(?!$value)";
}
$arr_xml_special_char_regex .= ")";
$pattern = "/$arr_xml_special_char_regex&([a-zA-Z0-9]+;)/";
$replacement = '&${1}';
return preg_replace($pattern, $replacement, $text);
}
?>