php如何去掉字符串里的符号
导读:本文共1224字符,通常情况下阅读需要4分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 方法:1、用“str_replace("指定符号","",$str)”去除指定符号;2、用“preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u",... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。
方法:1、用“str_replace("指定符号","",$str)”去除指定符号;2、用“preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u","$str",$result);”过滤掉全部符号。
本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
php去掉字符串里的符号
方法1:使用str_replace()函数--去除指定符号
str_replace()函数可以搜索字符串中的指定符号,并将其替换为空字符
例如:去掉?符号
<?phpheader("Content-type:text/html;charset=utf-8");$str="php.cn?。zblog,?#$%^&())*(&^";$newstr=str_replace("?","",$str);echo$newstr;?>
方法2:使用preg_match_all()函数
preg_match_all()函数可以配合正则表达式来过滤字符串,只保留中文、英文以及数字,去掉其他全部符号
<?phpheader("Content-type:text/html;charset=utf-8");$str="php.cn?。zblog,?#$%^&())*(&^";preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u","$str",$result);var_dump($result);?>
preg_match_all()函数接受一个数组类型的第三参数,用来存放所有匹配的结果。
可以使用join()函数将结果数组拼接成一个字符串
echojoin('',$result[0]);
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
php如何去掉字符串里的符号的详细内容,希望对您有所帮助,信息来源于网络。