Skip to content

Instantly share code, notes, and snippets.

@Clarence-pan
Created August 14, 2017 05:25
Show Gist options
  • Save Clarence-pan/dc4c2d59168b1daad6700c6923f535e3 to your computer and use it in GitHub Desktop.
Save Clarence-pan/dc4c2d59168b1daad6700c6923f535e3 to your computer and use it in GitHub Desktop.
将中文(unicode)编码成HTML实体编码,而保留ANSI字符
<?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>&#20013;&#22269;</title>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment