本篇文章是对PHP中的UNICODE 编码与解码进行了详细的分析介绍
需要的朋友参考下
方法一:
复制代码 代码如下:
<?php
function unicode_encode($name)
{
$name = iconv(
UTF
UCS
$name);
$len = strlen($name);
$str =
;
for ($i =
; $i < $len
; $i = $i +
)
{
$c = $name[$i];
$c
= $name[$i +
];
if (ord($c) >
)
{ //两个字节的文字
$str
=
u
base_convert(ord($c)
)
str_pad(base_convert(ord($c
)
)
STR_PAD_LEFT);
}
else
{
$str
= $c
;
}
}
return $str;
}
//将UNICODE编码后的内容进行解码
function unicode_decode($name)
{
//转换编码
将Unicode编码转换成可以浏览的utf
编码
$pattern =
/([w]+)|(u([w]{
}))/i
;
preg_match_all($pattern
$name
$matches);
if (!empty($matches))
{
$name =
;
for ($j =
; $j < count($matches[
]); $j++)
{
$str = $matches[
][$j];
if (strpos($str
u
) ===
)
{
$code = base_convert(substr($str
)
);
$code
= base_convert(substr($str
)
);
$c = chr($code)
chr($code
);
$c = iconv(
UCS
UTF
$c);
$name
= $c;
}
else
{
$name
= $str;
}
}
}
return $name;
}
方法二:
复制代码 代码如下:
function unicodeutf($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = [" $str "];
$decode = json_decode($str);
if(count($decode) == ){
return $decode[];
}
return $str;
}