PHP實(shí)現(xiàn)Unicode編碼相互轉(zhuǎn)換的方法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)Unicode編碼相互轉(zhuǎn)換的方法。分享給大家供大家參考,具體如下:
<?php
/**
* $str 原始中文字符串
* $encoding 原始字符串的編碼,默認(rèn)utf-8
* $prefix 編碼后的前綴,默認(rèn)"&#"
* $postfix 編碼后的后綴,默認(rèn)";"
*/
function unicode_encode($str, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
//將字符串拆分
$str = iconv("UTF-8", "gb2312", $str);
$cind = 0;
$arr_cont = array();
for ($i = 0; $i < strlen($str); $i++) {
if (strlen(substr($str, $cind, 1)) > 0) {
if (ord(substr($str, $cind, 1)) < 0xA1) { //如果為英文則取1個(gè)字節(jié)
array_push($arr_cont, substr($str, $cind, 1));
$cind++;
} else {
array_push($arr_cont, substr($str, $cind, 2));
$cind+=2;
}
}
}
foreach ($arr_cont as &$row) {
$row = iconv("gb2312", "UTF-8", $row);
}
//轉(zhuǎn)換Unicode碼
foreach ($arr_cont as $key => $value) {
$unicodestr.= $prefix . base_convert(bin2hex(iconv('utf-8', 'UCS-4', $value)), 16, 10) .$postfix;
}
return $unicodestr;
}
/**
* $str Unicode編碼后的字符串
* $decoding 原始字符串的編碼,默認(rèn)utf-8
* $prefix 編碼字符串的前綴,默認(rèn)"&#"
* $postfix 編碼字符串的后綴,默認(rèn)";"
*/
function unicode_decode($unistr, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
$arruni = explode($prefix, $unistr);
$unistr = '';
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('UCS-2', $encoding, $unistr);
}
$str = "PHP編程:www.dhdzp.com";
$unistr = unicode_encode($str);
$unistr2 = unicode_decode($unistr);
echo $unistr . '<br />';
echo $unistr2 . '<br />';
$unistr = unicode_encode($str,'GBK','\\u');
$unistr2 = unicode_decode($unistr,'GBK','\\u');
echo $unistr . '<br />';
echo $unistr2 . '<br />';
PS:下面測(cè)試過這個(gè)函數(shù)比較好用,該代碼需要在utf-8編碼環(huán)境下運(yùn)行
function unicode_encode($name) {//Unicode編碼
$jsonarr = array($name);
$jsonstr = json_encode($jsonarr);
if (empty ($jsonstr))
return '';
return substr($jsonstr,2,-2);
}
function unicode_decode($name) {//Unicode解碼
$json = '{"str":"' . $name . '"}';
$arr = json_decode($json, true);
if (empty ($arr))
return '';
return $arr['str'];
}
$test = "\u811a\u672c\u4e4b\u5bb6";
echo "unicode解碼:".unicode_decode($test)."<br/>";
echo "unicode編碼:".unicode_encode('腳本之家')."<br/>";
PS:這里再為大家提供幾款Unicode編碼轉(zhuǎn)換操作相關(guān)工具供大家參考使用:
在線Unicode/中文轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/unicode_chinese
Native/Unicode在線編碼轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/native2unicode
在線中文漢字/ASCII碼/Unicode編碼互相轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/chinese2unicode
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php正則表達(dá)式用法總結(jié)》、及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- Python3的unicode編碼轉(zhuǎn)換成中文的問題及解決方案
- js字符串與Unicode編碼互相轉(zhuǎn)換
- python實(shí)現(xiàn)unicode轉(zhuǎn)中文及轉(zhuǎn)換默認(rèn)編碼的方法
- JS實(shí)現(xiàn)的Unicode編碼轉(zhuǎn)換操作示例
- .Net(c#)漢字和Unicode編碼互相轉(zhuǎn)換實(shí)例
- C#將Unicode編碼轉(zhuǎn)換為漢字字符串的簡(jiǎn)單方法
- JavaScript中字符串與Unicode編碼互相轉(zhuǎn)換的實(shí)現(xiàn)方法
- PHP如何實(shí)現(xiàn)Unicode和Utf-8編碼相互轉(zhuǎn)換
- js unicode 編碼解析關(guān)于數(shù)據(jù)轉(zhuǎn)換為中文的兩種方法
- C++11 Unicode編碼轉(zhuǎn)換
相關(guān)文章
PHP入門教程之使用Mysqli操作數(shù)據(jù)庫的方法(連接,查詢,事務(wù)回滾等)
這篇文章主要介紹了PHP入門教程之使用Mysqli操作數(shù)據(jù)庫的方法,涉及php+mysqli操作數(shù)據(jù)庫的基本連接、編碼設(shè)置、查詢、修改、事務(wù)回滾等操作技巧,需要的朋友可以參考下2016-09-09
解決文件名解壓后亂碼的問題 將文件名進(jìn)行轉(zhuǎn)碼的代碼
中文win32下,文件名使用GBK編碼 Linux下,文件名使用UTF-8編碼 解決文件名解壓后亂碼的問題,使用將文件名進(jìn)行轉(zhuǎn)碼2012-01-01
PHP通過CURL實(shí)現(xiàn)定時(shí)任務(wù)的圖片抓取功能示例
這篇文章主要介紹了PHP通過CURL實(shí)現(xiàn)定時(shí)任務(wù)的圖片抓取功能,涉及php的curl及dom操作相關(guān)技巧,需要的朋友可以參考下2016-10-10
php相對(duì)當(dāng)前文件include其它文件的方法
這篇文章主要介紹了php相對(duì)當(dāng)前文件include其它文件的方法,實(shí)例分析了php中include引入文件的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
PHP+APACHE實(shí)現(xiàn)網(wǎng)址偽靜態(tài)
這篇文章主要介紹了PHP+APACHE通過使用mod rewrite模塊實(shí)現(xiàn)網(wǎng)址偽靜態(tài)的相關(guān)資料,需要的朋友可以參考下2015-02-02
php數(shù)值轉(zhuǎn)換時(shí)間及時(shí)間轉(zhuǎn)換數(shù)值用法示例
這篇文章主要介紹了php數(shù)值轉(zhuǎn)換時(shí)間及時(shí)間轉(zhuǎn)換數(shù)值用法,涉及php strtotime及date等函數(shù)實(shí)現(xiàn)日期的字符串格式與Unix時(shí)間戳格式轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

