The regular filter string, how to solve

Source: Internet
Author: User
Regular filter string

Chinese encoding contains UTF-8,GBK
$str = "New _ Jian & wen \ piece, clamp abd";
Get results
$res = "new Folder Abd";
Contains illegal characters:
$out [0] = "_";
$out [1] = "&";
$out [2] = "\";
$out [3] = "";
$out [4] = ",";
?>
Regular PHP

Share to:


------Solution--------------------
$str = "New _ Jian & wen \ piece, clamp abd";
echo preg_replace ('/[_&\\\,\s]+/u ', ', $str);

------Solution--------------------
Chinese encoding contains UTF-8,GBK
$str = "New _ Jian & wen \ piece, clamp abd";
Get results
$res = "new Folder Abd";
Contains illegal characters:
$out [0] = "_";
$out [1] = "&";
$out [2] = "\ \";
$out [3] = "";
$out [4] = ",";

$pattern = Join ('
------Solution--------------------
', Array_map (' Preg_quote ', $out));
echo preg_replace ("/$pattern/", "', $str);
New Folder Abd

------Solution--------------------
If the simple English letter is OK range, meet the requirements of [a-za-z0-9], that is, all other than illegal characters.
But if you add a mix of Chinese and English, there is no good way to deduct it unless you are sure of the range of kanji, for example GBK can usually use [\x80-\xff][\x40-\xfe] to denote the range of Chinese characters, note that this is only about the range, and if it is UTF8, it is \u4e00-\ U9fa5, there is no way to kill if there are some characters that you think are illegal.

Roughly speaking, the range of all illegal characters other than Chinese and English letters GBK should be [^a-za-z0-9\x80-\xff\x40-\xfe]
And the UTF8 is [^a-za-z0-9\x{4e00}-\x{9fff}]
------Solution--------------------
echo preg_replace ('/[^a-za-z\p{han}]+/u ', ', ' new _ Jian & wen \ piece, clip abd ');

------Solution--------------------
The GBK on the top of the book is wrong, look at the example of the amount can be known
 
  $test = ' New _ & ' pieces, clamp a ah [set [Abd ';

$matches = Array ();

$reg = "/[^a-za-z0-9\x80-\xff]/";

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

Var_dump ($matches);

$str = ' Set ';

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

Var_dump (Ord (' ['));


If it's GBK, you can do it the following way.

$test = ' New _ & ' pieces, clamp a ah [set [Abd ';


$out = Preg_split ('/([a-za-z0-9]
------Solution--------------------
[\x80-\xff].) +/', $test);



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.