淺析PHP中的UNICODE 編碼與解碼
更新時間:2013年06月29日 11:26:21 作者:
本篇文章是對PHP中的UNICODE 編碼與解碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
方法一:
<?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)
{ //兩個字節(jié)的文字
$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編碼后的內(nèi)容進(jìn)行解碼
function unicode_decode($name)
{
//轉(zhuǎn)換編碼,將Unicode編碼轉(zhuǎn)換成可以瀏覽的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;
}
方法二:
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
復(fù)制代碼 代碼如下:
<?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)
{ //兩個字節(jié)的文字
$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編碼后的內(nèi)容進(jìn)行解碼
function unicode_decode($name)
{
//轉(zhuǎn)換編碼,將Unicode編碼轉(zhuǎn)換成可以瀏覽的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;
}
方法二:
復(fù)制代碼 代碼如下:
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
您可能感興趣的文章:
- PHP解碼unicode編碼的中文字符代碼分享
- php utf-8轉(zhuǎn)unicode的函數(shù)
- PHP中正則表達(dá)式對UNICODE字符碼的匹配方法
- PHP如何實現(xiàn)Unicode和Utf-8編碼相互轉(zhuǎn)換
- php UTF-8、Unicode和BOM問題
- 簡單談?wù)刾hp中的unicode和utf8編碼
- php制作unicode解碼工具(unicode編碼轉(zhuǎn)換器)代碼分享
- 用php實現(xiàn)gb2312和unicode間的編碼轉(zhuǎn)換
- PHP解密Unicode及Escape加密字符串
- php實現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享
- PHP實現(xiàn)Unicode編碼相互轉(zhuǎn)換的方法示例
相關(guān)文章
PHP面向?qū)ο蠖鄳B(tài)性實現(xiàn)方法簡單示例
這篇文章主要介紹了PHP面向?qū)ο蠖鄳B(tài)性實現(xiàn)方法,簡單說明了面向?qū)ο蠖鄳B(tài)性的原理并結(jié)合具體實例給出了php實現(xiàn)多態(tài)性的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09php json_encode值中大括號與花括號區(qū)別
這篇文章主要介紹了json_encode值中大括號與花括號區(qū)別,具體的看下實例說明,需要的朋友參考下2013-09-09PHP生成指定范圍內(nèi)的N個不重復(fù)的隨機(jī)數(shù)
今天小編就為大家分享一篇關(guān)于PHP生成指定范圍內(nèi)的N個不重復(fù)的隨機(jī)數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03php使用vue實現(xiàn)省市區(qū)三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了php如何使用vue實現(xiàn)省市區(qū)三級聯(lián)動效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12