Home  >  Article  >  Backend Development  >  How to convert php utf8 to unicode

How to convert php utf8 to unicode

藏色散人
藏色散人Original
2020-11-02 09:06:042830browse

php utf8转unicode的方法:首先创建一个PHP示例文件;然后通过“iconv($encoding, 'UCS-2BE', $str);”方法实现转换即可。

How to convert php utf8 to unicode

推荐:《PHP视频教程

linux下php中文UTF-8转换Unicode方法和注意事项

先说下遇到问题:1.php没有内置unicode_ecode函数可以直接使用

2.网上很多资料都是用$str = iconv($encoding'UCS-2'$str);

window下转换出来的是正常的,但在Linux下转换出来的两个字符是相反的,用在线unicode转换工具出来的结果是乱码。

UCS-2的编码规则:
windows下默认是UCS-2LE。
linux下默认是UCS-2BE。用iconv(指定UCS-2)来转换生成的是UCS-2BE的unicode,但可能php环境配置会导致不是UCS-2BE。
windows和linux等多个平台对 UCS-2 的理解不同(UCS-2LE,UCS-2BE),所以为了统一需要直接指定为UCS-2BE。

即把:$str = iconv($encoding'UCS-2'$str); 改为<code class="php variable">$str = iconv($encoding'UCS-2BE'$str);

亲测转换出来的unicode可以正常转换的

下面是两个本人亲测可以使用的函数(为了避免以后跟系统新的内置函数同名在前面加了个my前缀):

/**
 * utf-8 转unicode
 * @param string $name
 * @return string
 */
function myutf8_unicode($name){
    $name = iconv(&#39;UTF-8&#39;, &#39;UCS-2BE&#39;, $name);
    $len  = strlen($name);
    $str  = &#39;&#39;;
    for ($i = 0; $i < $len - 1; $i = $i + 2){
        $c  = $name[$i];
        $c2 = $name[$i + 1];
        if (ord($c) > 0){
            $str .= &#39;\u&#39;.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
        } else {
            $str .= &#39;\u&#39;.str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
        }
    }
    return $str;
}

/**
 * unicode 转 utf-8
 *
 * @param string $name
 * @return string
 */
function myunicode_decode($name)
{
    $name = strtolower($name);
    // 转换编码,将Unicode编码转换成可以浏览的utf-8编码
    $pattern = &#39;/([\w]+)|(\\\u([\w]{4}))/i&#39;;
    preg_match_all($pattern, $name, $matches);
    if (! empty($matches)) {
        $name = &#39;&#39;;
        for ($j = 0; $j < count($matches[0]); $j ++) {
            $str = $matches[0][$j];
            if (strpos($str, &#39;\\u&#39;) === 0) {
                $code = base_convert(substr($str, 2, 2), 16, 10);
                $code2 = base_convert(substr($str, 4), 16, 10);
                $c = chr($code) . chr($code2);
                $c = iconv(&#39;UCS-2BE&#39;, &#39;UTF-8&#39;, $c);
                $name .= $c;
            } else {
                $name .= $str;
            }
        }
    }
    return $name;
}

测试代码:

$ustr = myutf8_unicode(&#39;我的新衣&#39;);
echo &#39;我的新衣:&#39;.$ustr.&#39;<br>&#39;;
$str = myunicode_decode($ustr);
echo $str.&#39;<br>&#39;;

输出结果:

在站长工具里可以正常转换,说明没有问题。

 

 

The above is the detailed content of How to convert php utf8 to unicode. 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