Home > Backend Development > PHP Problem > How to convert Chinese characters to unicode in php

How to convert Chinese characters to unicode in php

藏色散人
Release: 2023-03-01 20:42:02
Original
5475 people have browsed it

php汉字转unicode的方法:首先创建PHP示例代码文件;然后定义一个“unicode_encode”方法;接着再创建一个“unicode_decode”方法;最后通过定义好的方法进行转换即可。

How to convert Chinese characters to unicode in php

php汉字转unicode

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);
}
Copy after login

使用:

//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>
Copy after login

推荐:《PHP教程

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template