php實(shí)現(xiàn)正則替換特殊符號的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后通過正則表達(dá)式“preg_replace($regex,"",$other);”實(shí)現(xiàn)正則替換特殊符號即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
如果是只想輸入中文的話,就這么寫,要注意是分gb2312和utf-8的哦:
gb2312:if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)) utf-8 :if(!preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str))
去除特殊字符
$regex = "/(/|~|!|@|#|\$|%|^|&|*|(|)|_|+|{|}|:|<|>|?|[|]|,|.|/|;|'|`|-|=|\|||"){2,}/"; $other = preg_replace($regex,"",$other); $regex = "/(,|。|!|;|:|、|’|‘|【|】|《|》|?|@|#|¥|%|&|*|(|)|-|+|=|@|…){2,}/"; $other = preg_replace($regex,"",$other);
推薦學(xué)習(xí):《PHP視頻教程》