【发布时间】:2016-12-08 10:28:00
【问题描述】:
有没有办法确定特定编码中字符所需的最小字节数?就像 mbstring 扩展支持的编码之一。 UTF-8 的值为 1,UTF-16 的值为 2,等等。
我不想获取特定字符串或字符的长度。
我想知道给定编码支持的最小字符大小,根据它的规范。
我目前使用这个代码:
<?php
function flawed_detection($encoding)
{
// I use 'a' in the hope that this char need the least number of bytes in all the supported encodings
return strlen(mb_convert_encoding('a', $encoding, 'UTF-8'));
}
foreach (mb_list_encodings() as $encoding) {
echo "$encoding: ", flawed_detection($encoding), "\n";
}
部分输出:
...
UTF-16LE: 2
UTF-8: 1
UTF-7: 1
UTF7-IMAP: 1
ASCII: 1
EUC-JP: 1
...
但我不确定要使用的“正确”字符。如果有的话。
编辑:我已经在每种编码中使用从 0 到 U+10FFFF 的每个字符测试了蛮力方法,结果与我的 finally_not_so_flawed_detection 函数完全相同(使用 'a' 字符或带空格):p
【问题讨论】:
-
为什么?这里的目标是什么?您是否有正当的商业或技术理由不全面使用 UTF-8?
-
为什么?这是一个通用问题:p 我在我的项目中使用 UTF-8,但我需要解码二进制文件中的一些字符串。
-
python中相关的暴力破解方法:stackoverflow.com/questions/30870107/…
标签: php unicode encoding character-encoding mbstring