php unicode編碼和字符串互轉(zhuǎn)的方法
php字符串轉(zhuǎn)Unicode編碼, Unicode編碼轉(zhuǎn)php字符
百度了很多,都一樣, 要么不對(duì), 要不就是只是把字符串的漢字轉(zhuǎn)Unicode
經(jīng)過(guò)多次試驗(yàn)查找, 找到了如下方法,
注意:字符串編碼必須是utf-8,如果不是自行用icon轉(zhuǎn)一下
//字符串轉(zhuǎn)Unicode編碼 function unicode_encode($strLong) { $strArr = preg_split('/(?<!^)(?!$)/u', $strLong);//拆分字符串為數(shù)組(含中文字符) $resUnicode = ''; foreach ($strArr as $str) { $bin_str = ''; $arr = is_array($str) ? $str : str_split($str);//獲取字符內(nèi)部數(shù)組表示,此時(shí)$arr應(yīng)類似array(228, 189, 160) foreach ($arr as $value) { $bin_str .= decbin(ord($value));//轉(zhuǎn)成數(shù)字再轉(zhuǎn)成二進(jìn)制字符串,$bin_str應(yīng)類似111001001011110110100000,如果是漢字"你" } $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/', '$1$2$3', $bin_str);//正則截取, $bin_str應(yīng)類似0100111101100000,如果是漢字"你" $unicode = dechex(bindec($bin_str));//返回unicode十六進(jìn)制 $_sup = ''; for ($i = 0; $i < 4 - strlen($unicode); $i++) { $_sup .= '0';//補(bǔ)位高字節(jié) 0 } $str = '\\u' . $_sup . $unicode; //加上 \u 返回 $resUnicode .= $str; } return $resUnicode; } //Unicode編碼轉(zhuǎn)字符串方法1 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; } //Unicode編碼轉(zhuǎn)字符串 function unicode_decode2($str){ $json = '{"str":"' . $str . '"}'; $arr = json_decode($json, true); if (empty($arr)) return ''; return $arr['str']; } echo unicode_encode('若水小站:qq963087326'),'<br>'; //結(jié)果\u82e5\u6c34\u5c0f\u7ad9\u003a\u0071\u0071\u0039\u0036\u0033\u0030\u0038\u0037\u0033\u0032\u0036 echo unicode_decode('\u82e5\u6c34\u5c0f\u7ad9\u003a\u0071\u0071\u0039\u0036\u0033\u0030\u0038\u0037\u0033\u0032\u0036'); //結(jié)果若水小站:qq963087326
總結(jié)
到此這篇關(guān)于php unicode編碼和字符串互轉(zhuǎn)的方法的文章就介紹到這了,更多相關(guān)php unicode編碼和字符串互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iis6手工創(chuàng)建網(wǎng)站后無(wú)法運(yùn)行php腳本的解決方法
下面小編就為大家?guī)?lái)一篇iis6手工創(chuàng)建網(wǎng)站后無(wú)法運(yùn)行php腳本的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06php preg_match_all結(jié)合str_replace替換內(nèi)容中所有img
最近做站的時(shí)候,采集了大量的數(shù)據(jù),但采回來(lái)的數(shù)據(jù)基本上都要經(jīng)過(guò)過(guò)濾原站保留的數(shù)據(jù),其中IMG就是一個(gè)地方。網(wǎng)站上好多這些應(yīng)用例子似乎沒有必要“秀”出來(lái),但站已幾天沒寫日志,那就來(lái)一個(gè)吧2008-10-10php 使用ActiveMQ發(fā)送消息,與處理消息操作示例
這篇文章主要介紹了php 使用ActiveMQ發(fā)送消息,與處理消息操作,結(jié)合實(shí)例形式分析了php使用ActiveMQ實(shí)現(xiàn)消息的發(fā)送與接收處理相關(guān)操作技巧,需要的朋友可以參考下2020-02-02smarty巧妙處理iframe中內(nèi)容頁(yè)的代碼
最近在用smarty做一個(gè)小項(xiàng)目,發(fā)現(xiàn)smarty中模板引擎挺不錯(cuò),讓前端和后端真正的分離2012-03-03php使用simplexml_load_file加載XML文件并顯示XML的方法
這篇文章主要介紹了php使用simplexml_load_file加載XML文件并顯示XML的方法,實(shí)例分析了simplexml_load_file操作XML文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03PHP+sqlite數(shù)據(jù)庫(kù)操作示例(創(chuàng)建/打開/插入/檢索)
這篇文章主要介紹了PHP+sqlite數(shù)據(jù)庫(kù)操作的方法,簡(jiǎn)單分析了sqlite數(shù)據(jù)庫(kù)的功能及相關(guān)操作技巧,包括創(chuàng)建,打開,插入,檢索及錯(cuò)誤提示等,需要的朋友可以參考下2016-05-05關(guān)于session在PHP5的配置文件中的詳細(xì)設(shè)置參數(shù)說(shuō)明
關(guān)于session在PHP5的配置文件中的詳細(xì)設(shè)置參數(shù)說(shuō)明,需要的朋友可以參考下。2011-04-04