简体   繁体   English

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

[英]How do I write a regex in PHP to remove special characters?

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 az, AZ, or 0-9. 我想删除不是az,AZ或0-9的所有内容。 I'm looking for a reverse function of the above. 我正在寻找上述的反向功能。 A pseudocode way to write it would be 写一个伪代码的方式就是

If characters in $join are not equal to az,AZ,0-9 then change characters in $join to "_" 如果$ join中的字符不等于az,AZ,0-9则将$join字符更改为"_"

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

这应该可以解决问题。

The regular expression for anything which isn't az, AZ, 0-9 is: 任何非az,AZ,0-9的正则表达式是:

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

This is known as a Negated Character Class 这被称为否定字符类

The easiest way is this: 最简单的方法是:

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

\\W is the non-word character group. \\ W是非单词字符组。 A word character is az, AZ, 0-9, and _. 单词字符是az,AZ,0-9和_。 \\W matches everything not previously mentioned*. \\ 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. *编辑2:这假定为C或其中一个英语语言环境。 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. Unicode语言环境仅将代码点128下面的字符视为字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM