加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP实现Unicode编码相互转换的方法示例

发布时间:2020-12-12 22:06:59 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP实现Unicode编码相互转换的方法。供大家参考研究具体如下: 0) { if (ord(substr($str,1)) $value) { $unicodestr.= $prefix . base_convert(bin2hex(iconv('utf-8','UCS-4',$value)),16,10) .$postfix; } return $unicodestr;}/**

本篇章节讲解PHP实现Unicode编码相互转换的方法。分享给大家供大家参考,具体如下:

0) { if (ord(substr($str,1)) < 0xA1) { //如果为英文则取1个字节 array_push($arr_cont,substr($str,1)); $cind++; } else { array_push($arr_cont,2)); $cind+=2; } } } foreach ($arr_cont as &$row) { $row = iconv("gb2312","UTF-8",$row); } //转换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 原始字符串的编码,默认utf-8 * $prefix 编码字符串的前缀,默认"&#" * $postfix 编码字符串的后缀,默认";" */ function unicode_decode($unistr,$postfix = ';') { $arruni = explode($prefix,$unistr); $unistr = ''; for ($i = 1,$len = count($arruni); $i < $len; $i++) { if (strlen($postfix) > 0) { $arruni[$i] = substr($arruni[$i],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 = "www.52php.cn"; $unistr = unicode_encode($str); $unistr2 = unicode_decode($unistr); echo $unistr . '
'; echo $unistr2 . '
'; $unistr = unicode_encode($str,'GBK','u'); $unistr2 = unicode_decode($unistr,'u'); echo $unistr . '
'; echo $unistr2 . '
';

PS:这里再为大家提供几款Unicode编码转换操作相关工具供大家参考使用:

在线Unicode/中文转换工具:

Native/Unicode在线编码转换工具:

在线中文汉字/ASCII码/Unicode编码互相转换工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》、及《》

希望本文所述对大家PHP程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读