PHP judges string encoding and obtains Chinese characters in the string + clears Spaces

Source: Internet
Author: User
Reference URL: www. w3.orgInternationalquestionsqa-forms-utf-8.en.php $ resultpreg_match ('% ^ (? : [X09x0Ax0Dx20-x7E] # ASCII | [xC2-xDF] [x80-xBF] # non-overlong2-byte | xE0 [xA0-xBF] [x80-xBF] # excludingoverlongs | [xE1

Reference URL: http://www.w3.org/International/questions/qa-forms-utf-8.en.php $ result = preg_match ('% ^ (? : [\ X09 \ x0A \ x0D \ x20-\ x7E] # ASCII | [\ xC2-\ xDF] [\ x80-\ xBF] # non-overlong 2-byte | \ xE0 [\ xA0-\ xBF] [\ x80-\ xBF] # excluding overlongs | [\ xE1

URL: http://www.w3.org/International/questions/qa-forms-utf-8.en.php

$ Result = preg_match ('% ^ (? :
[\ X09 \ x0A \ x0D \ x20-\ x7E] # ASCII
| [\ XC2-\ xDF] [\ x80-\ xBF] # non-overlong 2-byte
| \ XE0 [\ xA0-\ xBF] [\ x80-\ xBF] # excluding overlongs
| [\ XE1-\ xEC \ xEE \ xEF] [\ x80-\ xBF] {2} # straight 3-byte
| \ XED [\ x80-\ x9F] [\ x80-\ xBF] # excluding surrogates
| \ XF0 [\ x90-\ xBF] [\ x80-\ xBF] {2} # planes 1-3
| [\ XF1-\ xF3] [\ x80-\ xBF] {3} # planes 4-15
| \ XF4 [\ x80-\ x8F] [\ x80-\ xBF] {2} # plane 16
) * $ % Xs ', $ string );

If $ result is true, it is a UTF-8-encoded string, no is ANSI

Match the Chinese characters in the string with the above conditions

If ($ result ){
Preg_match_all ("/[\ xE1-\ xEC \ xEE \ xEF] [\ x80-\ xBF] {2}/", $ str, $ arr );
Print_r ($ arr [0]);
} Else {
Preg_match_all ("/[\ x80-\ xFF]./", $ str, $ arr );
Print_r ($ arr [0]);
}

Zookeeper -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PHP clear spaces

During daily data processing, we often produce extra spaces. If you want to compare strings, this will lead to problems and waste additional storage space.

How do I remove spaces? Maybe you will first think of the PHP built-in function trim (). Yes, it does. However, in this case, it cannot handle the starting and ending parts of characters: Changing multiple spaces into one space, changing spaces into ordered queues, and so on...

Therefore, regular expressions are useful. Take a look at the following code:

$ Str = "This line contains \ tliberal \ r \ n use of whitespace. \ n ";

// First remove the leading and trailing Spaces
$ Str = trim ($ str );

// Remove the two or more spaces
$ Str = preg_replace ('/\ s (? = \ S)/', '', $ str );

// Replace a non-space with a space.
$ Str = preg_replace ('/[\ n \ r \ t]/', '', $ str );

The preceding example can remove all unnecessary spaces. Use TRim () to remove leading and trailing spaces, and then use preg_replace () to remove repeated spaces.
(? =) Indicates that only the following spaces are matched with the leading spaces.

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.