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.
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
[^a-zA-Z0-9_-]
should do it for you. Note the ^
inside the bracket.
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
How about a ^ character in front the characters:
preg_replace( '[^a-zA-Z0-9_-]", "", $var);