如何在PHP中编写正则表达式以删除特殊字符? [英] How do I write a regex in PHP to remove special characters?

查看:117
本文介绍了如何在PHP中编写正则表达式以删除特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP还是很陌生,我注意到处理正则表达式的方式有很多.

I'm pretty new to PHP, and I noticed there are many different ways of handling regular expressions.

这是我当前正在使用的:

This is what I'm currently using:

$replace = array(" ",".",",","'","@");
$newString = str_replace($replace,"_",$join);

$join = "the original string i'm parsing through";

我想删除所有非a-z,A-Z或0-9的内容.我正在寻找上面的反向功能.伪代码的写法是

I want to remove everything which isn't a-z, A-Z, or 0-9. I'm looking for a reverse function of the above. A pseudocode way to write it would be

如果$ join中的字符不等于a-z,A-Z,0-9 然后将$join中的字符更改为"_"

If characters in $join are not equal to a-z,A-Z,0-9 then change characters in $join to "_"

推荐答案

$newString = preg_replace('/[^a-z0-9]/i', '_', $join);

这应该可以解决问题.

这篇关于如何在PHP中编写正则表达式以删除特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆