PHP版 汉字转码的实现详解
内容摘要
如下所示:复制代码 代码如下:<?phpfunction unicode_encode($str, $encoding='GBK', $prefix='&#', $postfix=';'){ $str = iconv($encoding, 'UCS-2', $str); $arrstr = str
文章正文
如下所示:
<?php
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<$len; $i++)
{
$dec = hexdec(bin2hex($arrstr[$i]));
$unistr .= $prefix.$dec.$postfix;
}
return $unistr;
}
$str = '<b>哈哈</b>';
$unistr = unicode_encode($str);
echo $unistr.'<br />';
?>
复制代码 代码如下:
<?php
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<$len; $i++)
{
$dec = hexdec(bin2hex($arrstr[$i]));
$unistr .= $prefix.$dec.$postfix;
}
return $unistr;
}
$str = '<b>哈哈</b>';
$unistr = unicode_encode($str);
echo $unistr.'<br />';
?>
代码注释