Home >Backend Development >PHP Problem >How to replace symbols in php regular expression

How to replace symbols in php regular expression

藏色散人
藏色散人Original
2021-03-29 09:32:202007browse

php正则替换符号的方法:首先创建一个PHP示例文件;然后通过正则表达式“preg_replace($regex,"",$other);”去除特殊字符即可。

How to replace symbols in php regular expression

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php 正则替换特殊字符 和检测是否是中文

如果是只想输入中文的话,就这么写,要注意是分gb2312和utf-8的哦:

gb2312:if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/",$str)) 
utf-8 :if(!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str))

去除特殊字符

$regex = "/(\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\&#39;|\`|\-|\=|\\|\||\"){2,}/";
$other = preg_replace($regex,"",$other);
$regex = "/(,|。|!|;|:|、|’|‘|【|】|《|》|?|@|#|¥|%|&|\*|(|)|\-|\+|\=|@|…){2,}/";
$other = preg_replace($regex,"",$other);

  【推荐学习:PHP视频教程

The above is the detailed content of How to replace symbols in php regular expression. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn