Test the awareness code instance of encoding: {code ...} the result is strlen (), which calculates the number of bytes of the string. one English character is 1 byte, the Chinese character is 2 byte in GBK encoding, and the UTF8 is 3 byte, therefore, the number of bytes is 3*3 + 312mb_strlen () and mb_strin... test the awareness of encoding
Code example:
$ Str = 'Hello everyone ABC'; echo strlen ($ str); echo'
'; Echo mb_strlen ($ str, 'utf-8 ');
The result is 12, 6.
Strlen () calculates the number of bytes of a string. an English character is 1 byte, a Chinese character is 2 byte in GBK encoding, and UTF8 is 3 byte, therefore, the number of bytes is 3*3 + 3 = 12.
Mb_strlen () requires mb_string extension, mixed mb_strlen (string $ str [, string $ encoding = mb_internal_encoding ()]). The first parameter is the string to be checked, the second parameter is the encoding format. multi-byte characters are recorded as 1.
Test the awareness of encoding
Code example:
$ Str = 'Hello everyone ABC'; echo strlen ($ str); echo'
'; Echo mb_strlen ($ str, 'utf-8 ');
The result is 12, 6.
Strlen () calculates the number of bytes of a string. an English character is 1 byte, a Chinese character is 2 byte in GBK encoding, and UTF8 is 3 byte, therefore, the number of bytes is 3*3 + 3 = 12.
Mb_strlen () requires mb_string extension, mixed mb_strlen (string $ str [, string $ encoding = mb_internal_encoding ()]). The first parameter is the string to be checked, the second parameter is the encoding format. multi-byte characters are recorded as 1.
For more articles about the natural length of Chinese and English strings, refer to the PHP Chinese website!