Skip to content

Instantly share code, notes, and snippets.

@supermethod
Created April 11, 2011 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supermethod/913375 to your computer and use it in GitHub Desktop.
Save supermethod/913375 to your computer and use it in GitHub Desktop.
Convert HTML entities to XML entities - optionally coverts special characters
function xmlEntities($string, $specialchars = false) {
$translationTable = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
foreach ($translationTable as $char => $entity) {
$from[] = $entity;
$to[] = '&#'.ord($char).';';
}
//convert special characters &, ", ', <, > to html entities
if ($specialchars) {
$string = htmlspecialchars($string);
}
//convert html entities to XML entities
return str_replace($from, $to, $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment