如何确定字符所需的最小字节数?

Aye*_*ell 5 php unicode encoding character-encoding mbstring

有没有办法确定特定编码中字符所需的最小字节数?像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";
}
Run Code Online (Sandbox Code Playgroud)

部分输出:

...
UTF-16LE: 2
UTF-8: 1
UTF-7: 1
UTF7-IMAP: 1
ASCII: 1
EUC-JP: 1
...
Run Code Online (Sandbox Code Playgroud)

但我不确定要使用的"正确"字符.如果有的话.

编辑:我已经在每个编码中测试了从0到U + 10FFFF的每个字符的暴力方法,结果与我的finally_not_so_flawed_detection函数(使用'a'char或空格)完全相同:p

小智 1

我不知道有什么方法可以确定,但合理的近似方法是检查空格字符的宽度(" "、 U+20 等)。据我所知,每个正常的文本编码都支持该字符,并且每个可变长度编码都使用最小长度序列。