cod*_*rex 4 php string encoding utf-8
function seems_utf8($str) {
$length = strlen($str);
for ($i=0; $i < $length; $i++) {
$c = ord($str[$i]);
if ($c < 0x80) $n = 0; # 0bbbbbbb
elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
else return false; # Does not match any model
for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我从Wordpress获得了这个代码,我对此并不了解,但我想知道该功能究竟是什么.
如果有人知道请帮帮我吗?
我需要有关上述代码的清晰概念.如果逐行解释会更有帮助.
我使用两种方法来检查字符串是否为utf-8(取决于具体情况):
mb_internal_encoding('UTF-8'); // always needed before mb_ functions, check note below
if (mb_strlen($string) != strlen($string)) {
/// not single byte
}
Run Code Online (Sandbox Code Playgroud)
- 要么 -
if (preg_match('!\S!u', $string)) {
// utf8
}
Run Code Online (Sandbox Code Playgroud)
对于mb_internal_encoding - 由于php中的一些未知错误(版本5.3-(尚未在5.3上测试))将编码作为参数传递给mb_函数不起作用,并且需要先设置内部编码任何mb_函数的使用.
该算法基本上检查字节序列是否符合您在维基百科文章中可以看到的模式.
该for循环是要经过所有字节$str.ord获取当前字节的十进制数.然后测试该数字的某些属性.
如果数字小于128(0x80),则为单字节字符.如果它等于或大于128,则检查多字节字符的长度.这可以通过多字节字符序列的第一个字符来完成.如果第一个字节以110xxxxx,它是一个双字节字符; 1110xxxx,它是一个三字节字符,等等.
我认为最隐秘的部分是表达式($c & 0xE0) == 0xC0.那是检查二进制格式的数字是否具有某种特定模式.我将尝试解释它在同一个例子中是如何工作的.
由于我们为该模式测试的所有数字都等于或大于0x80,因此第一个字节始终为1,因此模式至少被限制为1xxxxxxxx.如果我们然后与11100000(0xE0)进行逐位比较,我们得到这个结果:
1xxxxxxx
& 11100000
= 1xx00000
Run Code Online (Sandbox Code Playgroud)
所以位置5和6的位(从右边读取,索引从0开始)取决于我们当前的数字.要使它等于11000000,第5位必须是0,第6位必须是1:
1xxxxxxx
& 11100000
? 11000000
??
? 110xxxxx
Run Code Online (Sandbox Code Playgroud)
这意味着我们号码的其他位可以是任意的:110xxxxx.而这正是维基百科文章中的模式预测的两个字节字符的第一个字节.
最后,内部for循环是检查多字节字符的后续字节的完整性.这一切都必须从一开始10xxxxxx.
如果您对UTF-8了解一点,那么这是一个非常简单的实现.
function seems_utf8($str) {
# get length, for utf8 this means bytes and not characters
$length = strlen($str);
# we need to check each byte in the string
for ($i=0; $i < $length; $i++) {
# get the byte code 0-255 of the i-th byte
$c = ord($str[$i]);
# utf8 characters can take 1-6 bytes, how much
# exactly is decoded in the first character if
# it has a character code >= 128 (highest bit set).
# For all <= 127 the ASCII is the same as UTF8.
# The number of bytes per character is stored in
# the highest bits of the first byte of the UTF8
# character. The bit pattern that must be matched
# for the different length are shown as comment.
#
# So $n will hold the number of additonal characters
if ($c < 0x80) $n = 0; # 0bbbbbbb
elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
else return false; # Does not match any model
# the code now checks the following additional bytes
# First if expression checks that the byte is really inside the
# string and not running over the string end.
# The second expression just check that the highest two bits of all
# additonal bytes are always 1 and 0 (hexadecimal 0x80)
# which is a requirement for all additional UTF-8 bytes
for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
顺便说说.在PHP上我假设这是一个比C函数慢50-100的因素,所以你不应该在长字符串和生产系统上真正使用它.
| 归档时间: |
|
| 查看次数: |
12282 次 |
| 最近记录: |