PHP 中的 preg_replace - NOT 条件的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4724732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preg_replace in PHP - regular expression for NOT condition
提问by Ali
I am trying to write a function in PHP using preg_replace where it will replace all those characters which are NOT found in list. Normally we replace where they are found but this one is different.
我正在尝试使用 preg_replace 在 PHP 中编写一个函数,它将替换列表中未找到的所有字符。通常我们会替换找到它们的位置,但这次不同。
For example if I have the string:
例如,如果我有字符串:
$mystring = "ab2c4d";
I can write the following function which will replace all numbers with *:
我可以编写以下函数,用 * 替换所有数字:
preg_replace("/(\d+)/","*",$mystring);
But I want to replace those characters which are neither number nor alphabets from a to z. They could be anything like #$*();~!{}[]|\/.,<>?' e.t.c.
但我想从 a 到 z 替换那些既不是数字也不是字母的字符。它们可以是 #$*();~!{}[]|\/.,<>?' 等等
So anything other than numbers and alphabets should be replaced by something else. How do I do that?
所以除了数字和字母之外的任何东西都应该用别的东西代替。我怎么做?
Thanks
谢谢
回答by Felix Kling
You can use a negated character class(using ^
at the beginning of the class):
/[^\da-z]+/i
Update:I mean, you have touse a negated character class and you canuse the one I provided but there are others as well ;)
更新:我的意思是,你必须使用一个否定的字符类,你可以使用我提供的一个,但还有其他的 ;)
回答by Dmitri
Try
尝试
preg_replace("/([^a-zA-Z0-9]+)/","*",$mystring);
回答by mario
You want to use a negated "character class". The syntax for them is [^...]
. In your case just [^\w]
I think.
您想使用否定的“字符类”。它们的语法是[^...]
. 在你的情况下,[^\w]
我只是想。
回答by Tim Pietzcker
\W
matches a non-alpha, non-digit character. The underscore _
is included in the list of alphanumerics, so it also won't match here.
\W
匹配非字母、非数字字符。下划线_
包含在字母数字列表中,因此它在此处也不匹配。
preg_replace("/\W/", "something else", $mystring);
should do if you can live with the underscore not being replaced. If you can't, use
如果您可以接受不被替换的下划线,应该这样做。如果不能,请使用
preg_replace("/[\W_]/", "something else", $mystring);
回答by Spudley
The \d
, \w
and similar in regex all have negative versions, which are simply the upper-case version of the same letter.
正则表达式中的\d
,\w
和类似的都有否定版本,它们只是同一个字母的大写版本。
So \w
matches any word character (ie basically alpha-numerics), and therefore \W
matches anything except a word character, so anything other than an alpha-numeric.
So\w
匹配任何单词字符(即基本上是字母数字),因此\W
匹配除单词字符以外的任何字符,因此匹配除字母数字字符以外的任何字符。
This sounds like what you're after.
这听起来像你所追求的。
For more info, I recommend regular-expressions.info.
有关更多信息,我推荐正则表达式.info 。
回答by MTK
Since PHP 5.1.0 can use \p{L}
(Unicode letters) and \p{N}
(Unicode digits) that is unicode equivalent like \d
and \w
for latin
自PHP 5.1.0起可以使用\p{L}
(Unicode字母)和\p{N}
(Unicode的数字)是Unicode的等价喜欢\d
和\w
拉美
preg_replace("/[^\p{L}\p{N}]/iu", $replacement_string, $original_string);
preg_replace("/[^\p{L}\p{N}]/iu", $replacement_string, $original_string);
/iu
modifiers at the end of pattern:
/iu
模式末尾的修饰符:
i (PCRE_CASELESS)
i (PCRE_CASELESS)
u (PCRE_UTF8)
see more at: https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
u (PCRE_UTF8)
查看更多:https: //www.php.net/manual/en/reference.pcre.pattern.modifiers.php