PHP to determine whether the input is pure digital, English, Chinese character method

Source: Internet
Author: User
Tags mixed php code strlen

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.