You can easily know the structure of the string by using php's mb_strlen and strlen functions.
Is it all in English, a mixture of English and Chinese, or pure Chinese characters. A brief description is as follows (above sample program)
1. If the character length returned by strlen is the same as the length calculated by mb_strlen based on the current encoding
Zhizhi, it can be judged to be a pure English string.
2. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
And the return value of strlen is the same as the return value of mb_strlen. If the remainder is 0, it can be judged to be a string of all Chinese characters.
3. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
And the return value of strlen is not 0 after the remainder of the return value of mb_strlen, it can be judged to be a mixed string of English and Chinese.
- /********
- Determine whether the input is pure numbers, English, Chinese characters, etc.
- Using php's mb_strlen and strlen functions, you can easily know the composition of the string
- whether it is all English, mixed English and Chinese, or pure Chinese characters. A brief description is as follows (above sample program)
- 1. If the character length returned by strlen is consistent with the length calculated by mb_strlen based on the current encoding, it can be judged to be a pure English string.
- 2. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
- and the remainder of the return value of strlen and the return value of mb_strlen is 0, it can be judged to be a string of all Chinese characters.
- 3. If the character length returned by strlen is inconsistent with the length calculated by mb_strlen based on the current encoding,
- and the return value of strlen is not 0 after the remainder of the return value of mb_strlen, it can be judged to be a mixed English-Chinese string.
- *
- * ****************/
- $str = "456abc";
- $x = mb_strlen($str,'gb2312');
- $y = strlen($str); echo "------456abc----
";
- echo "$x".'
';
- echo "$y".'
';
- $str = "456I am Chinese abc
";
- $x = mb_strlen($str,'gb2312');
- $y = strlen($str);
- echo "----- -456 I am Chinese abc----
";
- echo "$x".'
'; echo "$y".' ';
- $str = "I am Chinese and I love my motherland";
- $x = mb_strlen($str,'gb2312');
- $y = strlen($str);
- echo "------I am China I love my motherland----
"; echo "$x".' ';
- echo "$y".'
'; $str = "I";
- $x = mb_strlen($str,'gb2312');
- $y = strlen($str);
- echo "------I----
"; echo "$x".' ';
- echo "$y".'
';
- $str = "我ab";
- $x = mb_strlen($str,' gb2312');
- $y = strlen($str);
- echo "------I ab----
"; echo "$x".' '; echo "$y".' ';
- ?>
-
Copy code
|