PHP版 汉字转码的实现详解_php技巧
程序员文章站
2023-12-30 08:23:52
...
如下所示:
function unicode_encode($str, $encoding='GBK', $prefix='', $postfix=';'){
$str = iconv($encoding, 'UCS-2', $str);
$arrstr = str_split($str, 2);
$unistr = '';
for($i=0, $len=count($arrstr); $i {
$dec = hexdec(bin2hex($arrstr[$i]));
$unistr .= $prefix.$dec.$postfix;
}
return $unistr;
}
$str = '哈哈';
$unistr = unicode_encode($str);
echo $unistr.'
';
?>
复制代码 代码如下:
function unicode_encode($str, $encoding='GBK', $prefix='', $postfix=';'){
$str = iconv($encoding, 'UCS-2', $str);
$arrstr = str_split($str, 2);
$unistr = '';
for($i=0, $len=count($arrstr); $i {
$dec = hexdec(bin2hex($arrstr[$i]));
$unistr .= $prefix.$dec.$postfix;
}
return $unistr;
}
$str = '哈哈';
$unistr = unicode_encode($str);
echo $unistr.'
';
?>