Home  >  Article  >  Backend Development  >  How to convert php Chinese to unicode encoding

How to convert php Chinese to unicode encoding

藏色散人
藏色散人Original
2021-03-11 17:59:393800browse

php中文转unicode编码的方法:首先创建一个PHP示例文件;然后通过“function unicode_encode($str, $encoding = 'GBK', $prefix = '&#'...){...}”方法转换编码即可。

How to convert php Chinese to unicode encoding

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php汉字转Unicode编码函数

/**
 * $str 原始字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码后的前缀,默认"&#"
 * $postfix 编码后的后缀,默认";"
 */
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 Unicode编码后的字符串
 * $encoding 原始字符串的编码,默认GBK
 * $prefix 编码字符串的前缀,默认"&#"
 * $postfix 编码字符串的后缀,默认";"
 */
function unicode_decode($unistr, $encoding = &#39;GBK&#39;, $prefix = &#39;&#&#39;, $postfix = &#39;;&#39;) {
    $arruni = explode($prefix, $unistr);
    $unistr = &#39;&#39;;
    for($i = 1, $len = count($arruni); $i < $len; $i++) {
        if (strlen($postfix) > 0) {
            $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
        } 
        $temp = intval($arruni[$i]);
        $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
    } 
    return iconv(&#39;UCS-2&#39;, $encoding, $unistr);
}
使用:
//GBK字符串测试
$str = &#39;<b>哈哈</b>&#39;;
echo $str.&#39;<br />&#39;;
 
$unistr = unicode_encode($str);
echo $unistr.&#39;<br />&#39;; // &#60;&#98;&#62;&#21704;&#21704;&#60;&#47;&#98;&#62;
 
$str2 = unicode_decode($unistr);
echo $str2.&#39;<br />&#39;; //<b>哈哈</b>
 
//UTF-8字符串测试
$utf8_str = iconv(&#39;GBK&#39;, &#39;UTF-8&#39;, $str);
echo $utf8_str.&#39;<br />&#39;; // <b>鍝堝搱</b> 注:UTF在GBK下显示的乱码!可切换浏览器的编码测试
 
$utf8_unistr = unicode_encode($utf8_str, &#39;UTF-8&#39;);
echo $utf8_unistr.&#39;<br />&#39;; // &#60;&#98;&#62;&#21704;&#21704;&#60;&#47;&#98;&#62;
 
$utf8_str2 = unicode_decode($utf8_unistr, &#39;UTF-8&#39;);
echo $utf8_str2.&#39;<br />&#39;; // <b>鍝堝搱</b>
 
//其它后缀、前缀测试
$prefix_unistr = unicode_encode($str, &#39;GBK&#39;, "\\u", &#39;&#39;);
echo $prefix_unistr.&#39;<br />&#39;; // \u60\u98\u62\u21704\u21704\u60\u47\u98\u62
 
$profix_unistr2 = unicode_decode($prefix_unistr, &#39;GBK&#39;, "\\u", &#39;&#39;);
echo $profix_unistr2.&#39;<br />&#39;; //<b>哈哈</b>

【推荐学习:《PHP视频教程》】

The above is the detailed content of How to convert php Chinese to unicode encoding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn