/**
* 将内容进行UNICODE编码,如中文字符串"图片" 转为"\u56fe\u7247"
* @param string $name 要转换的中文字符串
* @param string $in_charset 输入中文编码,默认为uft8
* @param string $out_charset 输出unicode编码,'UCS-2BE'或'UCS-2LE'
* Linux 服务器上 UCS-2 编码方式与 Winodws 不一致,linux编码为UCS-2BE,windows为UCS-2LE,即big-endian和little-endian
* @return string
*/
if (!function_exists('unicode_encode')) {
function unicode_encode($name,$in_charset='UTF-8',$out_charset='UCS-2BE')
{
$name = iconv($in_charset, $out_charset, $name);
$len = strlen($name);
$str = '';
for ($i = 0; $i < $len - 1; $i = $i + 2){
$c = $name[$i];
$c2 = $name[$i + 1];
if (ord($c) > 0){ // 两个字节的文字
$str .= '\u'.base_convert(ord($c), 10, 16).base_convert(ord($c2), 10, 16);
}
else{
$str .= $c2;
}
}
return $str;
}
}
/**
* 将UNICODE编码后的内容进行解码,如unicode字符串:"\u56fe\u7247" 转为"图片"
* @param string $name 要转换的unicode字符串
* @param string $out_charset 输出编码,默认为utf8
* @param string $in_charset 输入unicode编码,Linux 服务器上 UCS-2 编码方式与 Winodws 不一致,
* linux编码为UCS-2BE,windows为UCS-2LE,即big-endian和little-endian
* @return string
*/
if (!function_exists('unicode_decode')) {
function unicode_decode($name,$out_charset='UTF-8',$in_charset='UCS-2BE')
{
// 转换编码,将Unicode编码转换成可以浏览的utf-8编码
$pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
preg_match_all($pattern, $name, $matches);
if (!empty($matches)){
$name = '';
for ($j = 0; $j < count($matches[0]); $j++){
$str = $matches[0][$j];
if (strpos($str, '\\u') === 0){
$code = base_convert(substr($str, 2, 2), 16, 10);
$code2 = base_convert(substr($str, 4), 16, 10);
$c = chr($code).chr($code2);
$c = iconv($in_charset, $out_charset, $c);
$name .= $c;
}
else{
$name .= $str;
}
}
}
return $name;
}
}