Php code used to determine whether a string is pure English, pure Chinese characters, or a mix of Chinese and English characters

Source: Internet
Author: User
Php code used to determine whether a string is pure English, pure Chinese characters, or a mix of Chinese and English characters
How can I use php code to determine the combination of strings? For example, how can we determine whether it is a mixture of pure English, pure numbers, or Chinese characters and English? Let's take a look at the analysis and examples provided in this article.

(PHP determines whether a string is a Chinese character (or English). In addition to regular expressions and splitting characters, PHP also provides a special method to determine whether the character value is less than 128.

That is, use the mb_strlen and strlen functions in php to determine: Use the above two functions to determine the return values of the characters with the current encoding, and then compare the return values. The returned values are English letters, numbers, and English numerals. The Returned values are not the same. The Returned values of strlen can be divisible by mb_strlen to pure Chinese characters. The return value is not equal, and the strlen return value cannot be divisible by mb_strlen in the English-Chinese or Chinese-English hybrid sorting.

What a wonderful function, how interesting an idea, and how wonderful an example are shown below.

Example,

     $ Value) {$ x = mb_strlen ($ value, 'gb2312'); $ y = strlen ($ value); echo $ strarray [$ key]. ''. $ x. ''. $ y. '';}?>

Output: hello 5 5123456 6 6123 hello 8 8 hello 2 4123 hello 5 7hello hello 7 9123hello 10 12

Php does not have a direct function to determine whether a string is pure English, pure Chinese characters, and a mix of Chinese and English characters. you can only write functions by yourself. To implement this function, you must understand the character set encoding placeholder. Currently, UTF8 and GBK are commonly used character sets in China.

UTF8 each Chinese character is equal to three lengths; GBK each Chinese character is equal to two lengths;

Based on the differences between Chinese characters and English letters, you can use the mb_strlen function and the strlen function to calculate two groups of length numbers respectively. then, you can determine the string type by performing regular operations.

1. example of UTF-8

     '; Function utf8_str ($ str) {$ mb = mb_strlen ($ str, 'utf-8'); $ st = strlen ($ str ); if ($ st = $ mb) return 'English only '; if ($ st % $ mb = 0 & $ st % 3 = 0) return 'Chinese characters'; return 'Chinese-English mixture ';} $ str = 'blog'; echo 'string :'. $ str. ', Yes '. utf8_str ($ str ). '';?>

2. example of the GBK method

     

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.