Home  >  Article  >  Backend Development  >  How to determine whether the input is pure numbers, English or Chinese characters in php_PHP tutorial

How to determine whether the input is pure numbers, English or Chinese characters in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:25811browse

How PHP determines whether the input is pure numbers, English, or Chinese characters.

The example in this article describes how PHP determines whether the input is pure numbers, English, or Chinese characters. Share it with everyone for your reference. The specific analysis is as follows:

Using php's mb_strlen and strlen functions here, you can easily know whether the string is composed of all English, mixed English and Chinese, or pure Chinese characters. A brief description is as follows:

1. If the character length returned by strlen is the same as the length calculated by mb_strlen based on the current encoding
It can be judged that it is 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, and the result is 0, which 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, so it can be judged to be a mixed English-Chinese string.

The php code is as follows:
Copy code The code is as follows: /********
Determine whether the input is pure numbers, English, Chinese characters, etc.
You can easily know the composition 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
Consistent, 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, and 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 remainder of the return value of strlen and the return value of mb_strlen is not 0, so it can be judged to be a mixed string of English and Chinese.
*/
$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 "------456I 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 Chinese and 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".'
';
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963844.htmlTechArticlephp method to determine whether the input is pure numbers, English, and Chinese characters. This article describes the example of php determining whether the input is pure numbers. Methods for numbers, English, and Chinese characters. Share it with everyone for your reference. Tool...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn