PHP版 汉字转码的实现详解
更新时间:2013年06月09日 15:51:06 作者:
本篇文章是对用php实现汉字转码进行了详细的分析介绍,需要的朋友参考下
如下所示:
<?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 />';
?>
相关文章
PHP+HTML+JavaScript+Css实现简单爬虫开发
这篇文章主要为大家详细介绍了PHP+HTML+JavaScript+Css实现简单爬虫开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-03-03PHP使用pear实现mail发送功能 windows环境下配置pear
这篇文章主要介绍在 windows环境下如何配置pear,PHP使用pear实现mail发送功能,感兴趣的小伙伴们可以参考一下2016-04-04
最新评论