Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Created February 9, 2017 14:42
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 rudiedirkx/4e6f0ec53a73c46a660c0e8bbe235890 to your computer and use it in GitHub Desktop.
Save rudiedirkx/4e6f0ec53a73c46a660c0e8bbe235890 to your computer and use it in GitHub Desktop.
<?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