Regex: Strip non alpha numeric or punctuation

前端 未结 3 421
轮回少年
轮回少年 2020-12-09 16:12

How can I use PHP to strip out all characters that are NOT alpha, numeric, space, or puncutation?

I\'ve tried the following, but it strip punctuation.



        
相关标签:
3条回答
  • 2020-12-09 16:16
    $str = trim($str);
    $str = trim($str, "\x00..\x1F");
    $str = str_replace(array( ""","'","&","<",">"),' ',$str);
    $str = preg_replace('/[^0-9a-zA-Z-]/', ' ', $str);
    $str = preg_replace('/\s\s+/', ' ', $str); 
    $str = trim($str);
    $str = preg_replace('/[ ]/', '-', $str);
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-09 16:36
    preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $str);
    

    Example:

    php > echo preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", "⟺f✆oo☃. ba⟗r!");
    foo. bar!
    

    \p{P} matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:

    preg_replace("/[^a-zA-Z0-9\s.?!]/", "", $str);
    
    0 讨论(0)
  • 2020-12-09 16:36

    You're going to have to list the punctuation explicitly as there is no shorthand for that (eg \s is shorthand for white space characters).

    preg_replace('/[^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?]/', '', $str);
    
    0 讨论(0)
提交回复
热议问题