Reputation: 55
I am looking for a way to preg_replace() every chars except "-"
preg_replace ('#[^.0-9a-z]+#i', '', $string);
I would clearly like to replace all bad chars except the a-Z0-9 AND "-"
Result done :
$string = preg_replace ('#[^.0-9a-z]/[^-]/+#i', '', $string);
Upvotes: 1
Views: 3224
Reputation: 91430
Just add the dash in the character class:
preg_replace ('#[^.0-9a-z-]+#i', '', $string);
// here ___^
Upvotes: 0
Reputation: 12806
preg_replace('/[^-]/', '', $string);
Will replace everything but -
.
[]
denotes the set of items, ^
is the not operator. So when you put in [^-]
you are saying "give me everything that is not in this set, which in this case is -
.
Upvotes: 1