php实现utf-8转unicode函数分享
更新时间:2015年01月06日 14:51:37 投稿:hebedich
这篇文章主要介绍了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);
}
相关文章
Laravel框架学习笔记(二)项目实战之模型(Models)
上一篇已经介绍开发环境的搭建,这篇将从项目实战开发,一步一步了解laravel框架。首先我们来了解下laravel框架的模型 (Models)2014-10-10Laravel 5框架学习之Eloquent (laravel 的ORM)
Laravel 的 Eloquent ORM 提供了漂亮、简洁的 ActiveRecord 实现来和数据库的互动。 每个数据库表会和一个对应的「模型」互动。在开始之前,记得把 config/database.php 里的数据库连接配置好。2015-04-04PHP中Fatal error session_start()错误解决步骤
这篇文章主要介绍了PHP中Fatal error session_start()错误解决步骤,着重于错误的排除步骤,一步一步排查下去,肯定可以解决这个错误,需要的朋友可以参考下2014-08-08
最新评论