Regex: Strip non alpha numeric or punctuation

前端 未结 3 422
轮回少年
轮回少年 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.

提交回复
热议问题