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 />';
?>
相關文章
Windows平臺實現PHP連接SQL Server2008的方法
這篇文章主要介紹了Windows平臺實現PHP連接SQL Server2008的方法,結合實例形式分析了Windows平臺PHP連接SQL Server2008所需的相關dll動態(tài)鏈接庫文件及相應的配置與使用方法,需要的朋友可以參考下2017-07-07PHP is_subclass_of函數的一個BUG和解決方法
這篇文章主要介紹了PHP is_subclass_of函數的一個BUG和解決方法,這個BUG存在于php5.3.7版本以前,并且針對interface方面,需要的朋友可以參考下2014-06-06實現在同一方法中獲取當前方法中新賦值的session值解決方法
這篇文章主要介紹了在同一方法中獲取當前方法中新賦值的session值解決方法,需要的朋友可以參考下2014-06-06