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 );