There is a problem with php calculating the byte length of the string in gbk format.

problem description

theoretically, the next Chinese in gbk format is 2 bytes. The number and English are 1 byte.
however, when judging the byte length on php, it is found that when the number comes after Chinese, the length of the number is omitted

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

mb_strlen ("voice", "GB2312")); / / byte length 4
mb_strlen (" voice 1 voice, "GB2312"); / / byte length 4
mb_strlen (" voice 12 voice, "GB2312"); / / byte length 5
mb_strlen (" 1 voice, "GB2312"); / / byte length 5
mb_strlen (" 1 I you 1, "GB2312"); / / byte length 6

what result do you expect? What is the error message actually seen?

Php
Jun.21,2021

when the PHP file encoding is consistent with the mb_strlen parameter, the return value is correct
if the PHP file encoding is not consistent with mb_strlen, the return will be different depending on the PHP file encoding

is explained as follows:
when the PHP file encoding is UTF-8

1 the actual bytes of speech 1 are: 0x31 0xE8 0xAF 0xAD 0xE9 0x9F 0xB3 0x31 you can see it with an editor that can see the hex format.
when you use GB2312 to calculate the multibyte length, first encode the actual bytes listed above according to GB2312
0x31 0xE8 0xAF 0xAD 0xE9 0x9F 0xB3 0x31 = > 1 Xuan?
so the calculated length is 5

other strings refer to the following figure

Menu