问题描述
PHP使用正则表达式过滤特殊的字符
function replaceSpecialChar($strParam){ $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\||\s+/"; return preg_replace($regex,"_",$strParam); } $str = "a(b)c(d)e~!@#$%^&*()_+{}:|<>?,./;'[]\-=`xtz"; $str = replaceSpecialChar($str); echo "$str\n";