如何在PHP中编写正则表达式以删除特殊字符?

时间:2023-01-13 22:24:52

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

我是PHP的新手,我注意到处理正则表达式的方法有很多种。

This is what I'm currently using:

这就是我目前使用的:

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

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

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

我想删除不是a-z,A-Z或0-9的所有内容。我正在寻找上述的反向功能。写一个伪代码的方式就是

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

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

4 个解决方案

#1


35  

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

This should do the trick.

这应该可以解决问题。

#2


13  

I am not giving you the answer but this tutorial is well worth its 10 minutes.

我没有给你答案,但本教程非常值得10分钟。

Link to Regular Expressions in PHP

链接到PHP中的正则表达式

#3


9  

The regular expression for anything which isn't a-z, A-Z, 0-9 is:

任何不是a-z,A-Z,0-9的正则表达式是:

preg_replace('/[^a-zA-Z0-9]/', "_", $join);

This is known as a Negated Character Class

这被称为否定字符类

#4


7  

The easiest way is this:

最简单的方法是:

preg_replace('/\W/', '_', $join);

\W is the non-word character group. A word character is a-z, A-Z, 0-9, and _. \W matches everything not previously mentioned*.

\ W是非单词字符组。单词字符是a-z,A-Z,0-9和_。 \ W匹配之前未提及的所有内容*。

Edit: preg uses Perl's regular expressions, documented in the perlman perlre document.

编辑:preg使用Perl的正则表达式,记录在perlman perlre文档中。

*Edit 2: This assumes a C or one of the English locales. Other locales may have accented letters in the word character class. The Unicode locales will only consider characters below code point 128 to be characters.

*编辑2:这假定为C或其中一个英语语言环境。其他语言环境可能在单词字符类中具有重音字母。 Unicode语言环境仅将代码点128下面的字符视为字符。

#1


35  

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

This should do the trick.

这应该可以解决问题。

#2


13  

I am not giving you the answer but this tutorial is well worth its 10 minutes.

我没有给你答案,但本教程非常值得10分钟。

Link to Regular Expressions in PHP

链接到PHP中的正则表达式

#3


9  

The regular expression for anything which isn't a-z, A-Z, 0-9 is:

任何不是a-z,A-Z,0-9的正则表达式是:

preg_replace('/[^a-zA-Z0-9]/', "_", $join);

This is known as a Negated Character Class

这被称为否定字符类

#4


7  

The easiest way is this:

最简单的方法是:

preg_replace('/\W/', '_', $join);

\W is the non-word character group. A word character is a-z, A-Z, 0-9, and _. \W matches everything not previously mentioned*.

\ W是非单词字符组。单词字符是a-z,A-Z,0-9和_。 \ W匹配之前未提及的所有内容*。

Edit: preg uses Perl's regular expressions, documented in the perlman perlre document.

编辑:preg使用Perl的正则表达式,记录在perlman perlre文档中。

*Edit 2: This assumes a C or one of the English locales. Other locales may have accented letters in the word character class. The Unicode locales will only consider characters below code point 128 to be characters.

*编辑2:这假定为C或其中一个英语语言环境。其他语言环境可能在单词字符类中具有重音字母。 Unicode语言环境仅将代码点128下面的字符视为字符。