I'm trying everything in a string BUT a certain pattern.
My pattern is:
$pattern = "/^[a-zA-Z0-9]+$/D";
And I want to do the following:
$string = preg_replace(anything but $pat开发者_Python百科tern, "", $string);
How do I do that?
Thanks!
Use the caret "^" in the character class to say you don't want these characters.
$pattern = "/^[^a-zA-Z0-9]+$/D";
Explanation
^ Negates the character class, causing it to match a single character not listed in the character class. (Specifies a caret if placed anywhere except after the opening [)
Source, regular-expressions.info
$pattern = /^[^a-zA-Z0-9]+$/D
The ^
between the square brackets, [
and ]
, will make the group match all not of them. This will match from beginning of string to end, I'm not sure wether you need that too.
$pattern = "[^a-zA-Z0-9]+?"
Would work matching all the parts of the string which does not get matched by the other pattern.
精彩评论