首页  >  文章  >  php教程  >  汉字转换成Unicode编码PHP程序

汉字转换成Unicode编码PHP程序

WBOY
WBOY原创
2016-05-25 16:43:321422浏览

下面来总结一些常用的汉字转换成Unicode编码PHP程序实现代码,我们只要了解到Unicode编码与gbk编码之间的内置转换原理即可了.

汉字转换成unicode方法,代码如下:

<?php
//将utf8编码的汉字转换为unicode
function htou($c) {
    $n = (ord($c[0]) & 0x1f) << 12;
    $n = (ord($c[1]) & 0x3f) << 6;
    $n = ord($c[2]) & 0x3f;
    return $n;
}
//在代码中隐藏utf8格式的字符串
function my_utf8_unicode($str) {
    $encode = &#39;&#39;;
    for ($i = 0; $i < strlen($str); $i) {
        if (ord(substr($str, $i, 1)) > 0xa0) {
            $encode.= &#39;&#&#39; . htou(substr($str, $i, 3)) . &#39;;&#39;;
            $i = 2;
        } else {
            $encode.= &#39;&#&#39; . ord($str[$i]) . &#39;;&#39;;
        }
    }
    return $encode;
}
echo my_utf8_unicode("哈哈ABC");
?>

汉字转换成unicode方法二,代码如下:

<?php
function getUnicode($word) {
    // 转UTF8
    $word0 = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $word);
    $word1 = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $word0);
    $word = ($word1 == $word) ? $word0 : $word;
    // 拆分汉字
    preg_match_all(&#39;#(?:[x00-x7F]|[xC0-xFF][x80-xBF]+)#s&#39;, $word, $array, PREG_PATTERN_ORDER);
    $return = array();
    // 转换
    foreach ($array[0] as $cc) {
        $arr = str_split($cc);
        $bin_str = &#39;&#39;;
        foreach ($arr as $value) {
            $bin_str.= decbin(ord($value));
        }
        $bin_str = preg_replace(&#39;/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/&#39;, &#39;$1$2$3&#39;, $bin_str);
        $return[] = &#39;&#&#39; . bindec($bin_str) . &#39;;&#39;;
    }
    return implode(&#39;&#39;, $return);
}
?>

函数用法,代码如下:

$word = '一个汉字转换成Unicode四字节编码的PHP函数。'; 

echo getUnicode($word); 

/* 

上述将输出如下结果: 

一个汉字转换成Un  

icode四字节编  

码的PHP函数。 

*/ 

这一组函数可以将汉字转成unicode编码,也可以将unicode解码成汉字. 

将汉字转成Unicode的函数,代码如下:

function uni_encode ($word) 
{ 
    $word0 = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $word); 
    $word1 = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $word0); 
    $word =  ($word1 == $word) ? $word0 : $word; 
    $word = json_encode($word); 
    $word = preg_replace_callback(&#39;/\u(w{4})/&#39;, create_function(&#39;$hex&#39;, &#39;return &#39;&#&#39;.hexdec($hex[1]).&#39;;&#39;;&#39;), substr($word, 1, strlen($word)-2)); 
    return $word; 
}

对Unicode编码进行解码的函数,代码如下:

function uni_decode ($uncode) 
{ 
    $word = json_decode(preg_replace_callback(&#39;/&#(\d{5});/&#39;, create_function(&#39;$dec&#39;, &#39;return &#39;\u&#39;.dechex($dec[1]);&#39;), &#39;"&#39;.$uncode.&#39;"&#39;)); 
    return $word; 
}


永久链接:

转载随意!带上文章地址吧。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn