How to solve regular expression filter string

Source: Internet
Author: User
Regular expression filter string & lt ;? Php // Chinese encoding contains UTF-8, GBK $ str & nbsp; = & nbsp; "New _ & amp; 文\ \, clip & nbsp; abd "; // get the result $ res & nbsp ;=& nbsp; "create a folder abd"; // contains invalid characters: $ out [0 Regular filter string

// Chinese encoding contains UTF-8, GBK
$ Str = "new _ & 文\ \, folder abd ";
// Obtain the result
$ Res = "creating a folder abd ";
// Contains invalid characters:
$ Out [0] = "_";
$ Out [1] = "&";
$ Out [2] = "\";
$ Out [3] = "";
$ Out [4] = ",";
?>
Regular PHP sharing:
------ Solution --------------------
$ Str = "new _ & 文\ \, folder abd ";
Echo preg_replace ('/[_ \\\, \ s] +/U', '', $ str );

------ Solution --------------------
// Chinese encoding contains UTF-8, GBK
$ Str = "new _ & 文\ \, folder abd ";
// Obtain the result
$ Res = "creating a folder abd ";
// Contains invalid characters:
$ Out [0] = "_";
$ Out [1] = "&";
$ Out [2] = "\\";
$ Out [3] = "";
$ Out [4] = ",";

$ Pattern = join ('
------ Solution --------------------
', Array_map ('preg _ quote', $ out ));
Echo preg_replace ("/$ pattern/", '', $ str );
Create a folder named abd

------ Solution --------------------
If the English letters are good to determine the range, meet the requirements of the [A-Za-z0-9], that is, all other illegal characters.
However, if you add a mix of Chinese and English characters, unless you determine the range of all Chinese characters, there is no good way to deduct it, for example, GBK can usually use [\ x80-\ xff] [\ x40-\ xfe] to indicate the range of Chinese characters. Note that this is only the approximate range. if it is utf8, it is \ u4e00-\ u9fa5. if there are some characters that you think are illegal, there is no way to kill them.

Roughly speaking, the range of all illegal characters other than Chinese and English letters in gbk should be [^ A-Za-z0-9 \ x80-\ xff \ x40-\ xfe]
Utf8 is [^ A-Za-z0-9 \ x {4e00}-\ x {9fff}]
------ Solution --------------------
Echo preg_replace ('/[^ A-Za-z \ p {Han}] +/U', ''," _ & 文\ \, abd ");

------ Solution --------------------
The above gbk regular expression is wrong. you can see it in the example.
 $ Test = 'new_document, folder a [folder [abd ';

$ Matches = array ();

$ Reg = "/[^ a-zA-Z0-9 \ x80-\ xff]/";

Preg_match_all ($ reg, $ test, $ matches );

Var_dump ($ matches );

$ Str = 'hangzhou ';

Var_dump (ord ($ str [0]);
Var_dump (ord ($ str [1]);

Var_dump (ord ('['));


If it is gbk, you can use the following method

$ Test = 'new_document, folder a [folder [abd ';


$ Out = preg_split ('/([a-zA-Z0-9]
------ Solution --------------------
[\ X80-\ xff].) +/', $ test );

$ Matches = array ();

Preg_match_all ('/([a-zA-Z0-9]
------ Solution --------------------
[\ X80-\ xff].)/', $ test, $ matches );

$ Res = implode ($ matches [1]);


Var_dump ($ res );

$ Out = str_split (str_replace ($ matches [1], '', $ test ));

Var_dump ($ out );

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.