PHP中Unicode编码转换为UTF-8字符
2016-05-29 14:07
1096 查看
/** * utf8字符转换成Unicode字符 * @param [type] $utf8_str Utf-8字符 * @return [type] Unicode字符 */ function utf8_str_to_unicode($utf8_str) { $unicode = (ord($utf8_str[0]) & 0x1F) << 12; $unicode |= (ord($utf8_str[1]) & 0x3F) << 6; $unicode |= (ord($utf8_str[2]) & 0x3F); return dechex($unicode); } /** * Unicode字符转换成utf8字符 * @param [type] $unicode_str Unicode字符 * @return [type] Utf-8字符 */ function unicode_to_utf8($unicode_str) { $code = intval(hexdec($unicode_str)); //这里注意转换出来的code一定得是整形,这样才会正确的按位操作 $ord_1 = decbin(0xE0 | ($code >> 12)); $ord_2 = decbin(0x80 | (($code >> 6) & 0x3F)); $ord_3 = decbin(0x80 | ($code & 0x3F)); $utf8_str = chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3)); return $utf8_str; }
原文地址:PHP实现Unicode和UTF-8编码的互相转换
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- rem : web app适配的秘密武器
- php7 读取php.ini[4]
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- ASP编码必备的8条原则
- apache 环境下 php 的配置注意事项
- XML指南——XML编码