Home >Backend Development >PHP Tutorial >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:011183browse

How to determine whether the input is pure numbers, English, and Chinese characters in PHP

This article mainly introduces the method of PHP to determine whether the input is pure numbers, English, and Chinese characters, involving PHP The usage skills of functions such as mb_strlen and strlen have certain reference value. Friends in need can refer to it

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 equal to 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 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 string of English and Chinese.

The php code is as follows:

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
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 equal to 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 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 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/963990.htmlTechArticlephp method to determine whether the input is pure numbers, English, or Chinese characters. This article mainly introduces the method of php to determine whether the input is pure numbers, English, or Chinese characters. Methods of pure numbers, English, and Chinese characters, involving mb_strlen and strlen in php, etc...
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