谈谈php中的unicode和utf8编码
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function utf8_bytes(str)
{
var len = 0, unicode;
for(var i = 0; i < str.length; i++)
{
unicode = str.charCodeAt(i);
if(unicode < 0x0080) {
++len;
} else if(unicode < 0x0800) {
len += 2;
} else if(unicode <= 0xFFFF) {
len += 3;
}else {
throw "characters must be USC-2!!"
}
}
return len;
}
#例子
utf8_bytes('asdasdas')
8
utf8_bytes('yrt燕睿涛')
12
后台
?
1
2
3
4
#对于GBK字符串
$len = ceil(strlen(bin2hex(iconv('GBK', 'UTF-8', $word)))/2);
#对于UTF8字符串
$len = ceil(strlen(bin2hex($word))/2);
以上所述就是本文的全部内容了,希望大家能够喜欢。
【谈谈php中的unicode和utf8编码】相关文章:
php中php://input和$-POST有什么不同09-12
谈谈关于php的优点与缺点08-14
php的header和asp中的redirect比较09-20
PHP中的Trait09-17
PHP 和 MYSQL09-27
Java和PHP的区别09-03
PHP中的表单处理09-10