Unicode編碼解碼在線轉換工具


Unicode編碼解碼在線轉換工具

Unicode 是基於通用字符集(Universal Character Set)的標准來發展,並且同時也以書本的形式(The Unicode Standard,目前第五版由Addison-Wesley Professional出版,ISBN-10: 0321480910)對外發表。

2006年7月的最新版本的 Unicode 是5.0版本。 2005年3月31日推出的Unicode 4.1.0 。另外,5.0 Beta於2005年12月12日推出,5.2版本(unicode standard)於2009年10月1日正式推出,以供各會員評價。
目前Unicode標准,6.1版已發布(2012年1月31日)。在unicode聯盟網站上可以查看完整的6.1的核心規范。
Unicode定義了大到足以代表人類所有可讀字符的字符集。

 

 請輸入要編碼的內容:



 

請輸入要解碼的內容
 

 

PHP 代碼實現

復制代碼
<?php
function unicode_encode($name)
{
    $name = iconv('UTF-8', 'UCS-2', $name);
    $len = strlen($name);
    $str = '';
    for ($i = 0; $i < $len - 1; $i = $i + 2)
    {
        $c = $name[$i];
        $c2 = $name[$i + 1];
        if (ord($c) > 0)
        {   //兩個字節的文字
            $str .= '\u'.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
        }
        else
        {
            $str .= $c2;
        }
    }
    return $str;
}

//將UNICODE編碼后的內容進行解碼
function unicode_decode($name)
{
    //轉換編碼,將Unicode編碼轉換成可以瀏覽的utf-8編碼
    $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
    preg_match_all($pattern, $name, $matches);
    if (!empty($matches))
    {
        $name = '';
        for ($j = 0; $j < count($matches[0]); $j++)
        {
            $str = $matches[0][$j];
            if (strpos($str, '\\u') === 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('UCS-2', 'UTF-8', $c);
                $name .= $c;
            }
            else
            {
                $name .= $str;
            }
        }
    }
    return $name;
}
復制代碼

 

轉載:http://www.cnblogs.com/txw1958/archive/2013/04/20/unicode-encode-decode.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM