This article mainly introduces whether the PHP input is a pure digital, English, Chinese character method, involving PHP mb_strlen and strlen functions such as the use of skills, with a certain reference value, the need for friends can refer to the
-->
This article is an example of how PHP determines whether input is a pure digit, English, Chinese character. Share to everyone for your reference. The specific analysis is as follows:
Here, using PHP's Mb_strlen and strlen functions, it is easy to know that the composition of strings is all English, English-Chinese mixed, or pure Chinese characters. The brief description is as follows:
1. If strlen returns the length of the character and the length of the Mb_strlen calculated at the current encoding
To, can be judged to be a pure English string.
2. If the character length returned by the strlen is inconsistent with the length of the Mb_strlen calculated at the current encoding,
and strlen return value and Mb_strlen return value after the remainder of the 0 can be judged to be a full Chinese character string.
3. If the character length returned by the strlen is inconsistent with the length of the Mb_strlen calculated at the current encoding,
The strlen return value is not 0 after the return value of Mb_strlen, which can be judged as a mixed string of English and Chinese.
The PHP code is as follows:
The code is as follows: <?php
/********
To determine whether the input is pure digital, English, Chinese characters, etc.
Use PHP's Mb_strlen and strlen functions to easily know the composition of strings
Is the whole English, English-Chinese mixed, or pure Chinese characters. Briefly described below (sample program above)
1. If strlen returns the length of the character and the length of the Mb_strlen calculated at the current encoding
To, can be judged to be a pure English string.
2. If the character length returned by the strlen is inconsistent with the length of the Mb_strlen calculated at the current encoding,
and strlen return value and Mb_strlen return value after the remainder of the 0 can be judged to be a full Chinese character string.
3. If the character length returned by the strlen is inconsistent with the length of the Mb_strlen calculated at the current encoding,
The strlen return value is not 0 after the return value of Mb_strlen, which can be judged as a mixed string of English and Chinese.
*/
$str = "456ABC";
$x = Mb_strlen ($str, ' gb2312 ');
$y = strlen ($STR); echo "------456abc----<br>";
echo "$x". <br/> ';
echo "$y". <br/> ';
$str = "456 I am Chinese abc<br/>";
$x = Mb_strlen ($str, ' gb2312 ');
$y = strlen ($STR);
echo "------456 I am Chinese ABC----<br/>";
echo "$x". <br/> '; echo "$y". <br/> ';
$str = "I am a Chinese I love the Motherland";
$x = Mb_strlen ($str, ' gb2312 ');
$y = strlen ($STR);
echo "------I am a Chinese I love the motherland----<br/>"; echo "$x". <br/> ';
echo "$y". <br/> '; $str = "I";
$x = Mb_strlen ($str, ' gb2312 ');
$y = strlen ($STR);
echo "------i----<br/>"; echo "$x". <br/> ';
echo "$y". <br/> ';
$STR = "I ab";
$x = Mb_strlen ($str, ' gb2312 ');
$y = strlen ($STR);
echo "------Me ab----<br/>"; echo "$x". <br/> '; echo "$y". <br/> ';
?>
I hope this article will help you with your PHP program design.