php中文和unicode互转

unicode转中文时可以使用json_decode()函数实现。 中文转unicode时需要对字符串转换成UCS-4编码,再转成16进制,再从16进制转换成10进制加上&#前缀来实现中文转unicode编码。 ### unicode转中文 function unicodeDecode($unicode_str){ $json = '{"str":"'.$unicode_str.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; } ### 中文转unicode function UnicodeEncode($str){ //split word preg_match_all('/./u',$str,$matches); $unicodeStr = ""; foreach($matches[0] as $m){ //拼接 $unicodeStr .= "&#".base_convert(bin2hex(iconv('UTF-8',"UCS-4",$m)),16,10); } return $unicodeStr; }
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3