Stripping out characters that aren't a-zA-Z0-9

2019-07-23 03:31发布

问题:

I want to stop out all characters that do NOT match this regex pattern: [a-zA-Z0-9_-].

Usually I would do this:

preg_replace("[a-zA-Z0-9_-]", "", $var);

but obviously this has the oposite effect to what I want. Is there a NOT in regex? How can I get this to strip out any characters that do not match the pattern?

Thanks.

回答1:

This:

preg_replace("[a-zA-Z0-9_-]", "", $var);

wouldn't even replace those characters, except if the input string is exactly the pattern. By using [] as delimiters, they have not the same effect as their would in the expression itself. You could change your delimiter (e.g.: /), or add some more brackets in the pattern:

preg_replace("/[a-zA-Z0-9_-]/", "", $var);    // this works
preg_replace("[[a-zA-Z0-9_-]]", "", $var);    // this too

Now, to negate a pattern in [], you use ^ at the beginning:

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

You could also have used the insensitive modifier i to match both lowercase (a-z) and uppercase (A-Z):

preg_replace("/[^a-z0-9_-]/i", "", $var);   // same as above


回答2:

[^a-zA-Z0-9_-] should do it for you. Note the ^ inside the bracket.



回答3:

In a range element of the regular expression, if the first character is ^, it inverts the range (hence [^a-zA-Z0-9_-] matches any character that's not alphanumeric, underscore or dash)

This is true with most types of regular expressions



回答4:

How about a ^ character in front the characters:

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