php实现utf-8转unicode函数分享
2015-01-24信息快讯网
这篇文章主要介绍了php实现utf-8转unicode函数分享,需要的朋友可以参考下
代码很简单,功能却很实用,推荐给大家。
奉上代码先:
public function utf8_unicode($str) {
$unicode = array();
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen( $str ); $i++ ) {
$thisValue = ord( $str[ $i ] );
if ( $thisValue < ord('A') ) {
// exclude 0-9
if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
// number
$unicode[] = chr($thisValue);
}
else {
$unicode[] = '%'.dechex($thisValue);
}
} else {
if ( $thisValue < 128) {
$unicode[] = $str[ $i ];
} else {
if ( count( $values ) == 0 ) {
$lookingFor = ( $thisValue < 224 ) ? 2 : 3;
}
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$number = dechex($number);
$unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;
$values = array();
$lookingFor = 1;
} // if
} // if
}
} // for
return implode("",$unicode);
}
php通过Chianz.com获取IP地址与地区的方法
PHP中new static() 和 new self() 的区别介绍
php判断当前用户已在别处登录的方法
windows中为php安装mongodb与memcache
ioncube_loader_win_5.2.dll的错误解决方法
php模仿asp Application对象在线人数统计实现方法
PHP中使用xmlreader读取xml数据示例
dedecms集成财付通支付接口
为PHP5.4开启Zend OPCode缓存
PHP中require和include路径问题详解
腾讯微博提示missing parameter errorcode 102 错误的解决方法
thinkphp中session和cookie无效的解决方法
yiic命令时提示“php.exe”不是内部或外部命令的解决方法
PHP中使用file_get_contents抓取网页中文乱码问题解决方法
php中magic_quotes_gpc对unserialize的影响分析
php使用pdo连接报错Connection failed SQLSTATE的解决方法
php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法
Codeigniter框架实现获取分页数据和总条数的方法
Yii不依赖Model的表单生成器用法实例
Codeigniter购物车类不能添加中文的解决方法
PHP中array_slice函数用法实例详解
php中call_user_func函数使用注意事项
PHP检测字符串是否为UTF8编码的常用方法
PHP中UNIX时间戳和日期间的转换与计算实例
PHP中round()函数对浮点数进行四舍五入的方法
php使用function_exists判断函数可用的方法
PHP实现UTF-8文件BOM自动检测与移除实例
php使用fopen创建utf8编码文件的方法
PHP与MYSQL中UTF8 中文排序示例代码
PHP与MYSQL中UTF8编码的中文排序实例
PHP解码unicode编码的中文字符代码分享