手机
当前位置:查字典教程网 >编程开发 >php教程 >php实现utf-8转unicode函数分享
php实现utf-8转unicode函数分享
摘要:代码很简单,功能却很实用,推荐给大家。奉上代码先:复制代码代码如下:publicfunctionutf8_unicode($str){$un...

代码很简单,功能却很实用,推荐给大家。

奉上代码先:

复制代码 代码如下:

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)"u".$number;

$values = array();

$lookingFor = 1;

} // if

} // if

}

} // for

return implode("",$unicode);

}

【php实现utf-8转unicode函数分享】相关文章:

不用iconv库的gb2312与utf-8的互换函数

php的计数器程序

php实现的mongodb操作类

php序列化函数serialize() 和 unserialize() 与原生函数对比

php备份数据库类分享

用php实现gb2312和unicode间的编码转换

PHP实现分页的一个示例

php自定义hash函数实例

php实现ping

php简单实现多字节字符串翻转的方法

精品推荐
分类导航