Created
August 14, 2017 05:25
-
-
Save Clarence-pan/dc4c2d59168b1daad6700c6923f535e3 to your computer and use it in GitHub Desktop.
将中文(unicode)编码成HTML实体编码,而保留ANSI字符
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 | |
function html_entity_encode_unicode($str) { | |
$str = mb_convert_encoding($str , 'UTF-32', 'UTF-8'); | |
$t = unpack("N*", $str); | |
$t = array_map(function($n) { | |
return $n < 128 ? chr($n) : "&#$n;"; | |
}, $t); | |
return implode("", $t); | |
} | |
// // usage: | |
// echo html_entity_encode_unicode('<title>中国</title>'); | |
// // output: | |
// <title>中国</title> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment