/**
* utf8字符转换成Unicode字符
*
* @param $utf8_str
* @return string
*/
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 $unicode_str
* @return string
*/
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;
}
推荐: 浮生无事的博客
网友评论