If a fatalerror: calltoundefinedfunctionmb_strlen occurs when using mb_strlen, you can use phpinfo () to check whether mbstring is loaded. if not, try to copy php_mbstringdll to the windows directory.
If fatal error: call to undefined function mb_strlen occurs when using mb_strlen, you can use php info () to check whether mbstring is loaded. if not, try to copy php_mbstring.dll to the windows directory.
Utf-8 file encoding
- $ Var = 'ABC ';
- Mb_strlen ($ var, 'utf-8'); // output 7. both Chinese and English occupies one byte.
- Mb_strlen ($ var); // output 15 Chinese characters in three bytes. English occupies one byte.
- Mb_strlen ($ var, 'gbk'); // output 9 is abnormal
Retrieve all Chinese characters
- Function utf8substr ($ str, $ from, $ len)
- {
- Return preg_replace ('# ^ (? : [X00-x7f] | [xc0-xff] [x80-xbf] +) {0, '. $ from .'}'.
- '((? : [X00-x7f] | [xc0-xff] [x80-xbf] +) {0, '. $ len.'}). * # s ',
- '$ 1', $ str );
- }
Intercept code between Chinese and English
- Function gb2312_strlen ($ string)
- {
- $ Str_len = strlen ($ string );
- $ Str_count = 0;
- For ($ j = 0; $ j <$ str_len; $ j ++)
- {
- If (ord ($ string {$ j}) <127)
- {
- $ Str_count + = 1;
- Continue;
- }
- Else
- {
- If (ord ($ string {$ j + 1})> 127)
- {
- $ Str_count + = 1;
- $ J ++;
- Continue;
- }
- Else
- {
- $ Str_count + = 1;
- Continue;
- }
- }
- }
- Return $ str_count;
- }
- $ Str = "enable s d ";
- Echo gb2312_strlen ($ str );