Created
February 9, 2017 14:42
-
-
Save rudiedirkx/4e6f0ec53a73c46a660c0e8bbe235890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts UTF-8 to XML-safe HTML entities: Изложение => Изложение | |
*/ | |
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