Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Converts UTF-8 to XML-safe HTML entities: Изложение => &#1048;&#1079;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1077;
*/
function xmlencode($string) {
$string = (string) $string;
// Replace < and ' and & etc.
$string = htmlspecialchars($string, ENT_XML1 | ENT_QUOTES);
// Replace ë etc.
$string = preg_replace_callback('#[\x{0080}-\x{00ff}]#u', function($match) {
return '&#' . ord(utf8_decode($match[0])) . ';';
}, $string);
// Replace Russian etc.
$string = mb_convert_encoding($string, 'HTML-ENTITIES');
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.