php实现utf-8转unicode函数分享
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') ) {
/pic/p>
if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
/pic/p>
$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;
} /pic/p>
} /pic/p>
}
} /pic/p>
return implode("",$unicode);
}
【php实现utf-8转unicode函数分享】相关文章:
PHP如何实现Unicode和Utf-8编码相互转换07-31
php字符转URL地址的函数02-14
php制作unicode解码工具10-13
分享php过滤XSS攻击的函数11-02
php实现求相对时间函数02-21
PHP与UTF-8的最佳实践02-07
浅析PHP中的UNICODE编码与解码12-23
php使用ftp函数实现简单上传功能02-07
php自定义函数实现汉字分割替换01-06