I have a little problem with regex. I want to clean up a string.
Currently I'm using the following regex:
preg_replace("/[^a-zA-Z0-9 _-]/", "", "Example1:2@32");
which gives me: "Example1232"
But the colon shouldn't be replaced, too. I tried it already with:
preg_replace("/[^a-zA-Z0-9 _-:]/", "", "Example1:2@32");
but that doesn't work. Can somebody help me?
You have to escape -
character
preg_replace("/[^a-zA-Z0-9 _\-:]/", "", "Example1:2@32"));
Either method should work:
-
or-
at the end of your regex, since inbetween two other characters it has a certain meaning (namely a range, as in a-z
).