Titus
Titus

Reputation: 55

PREG_REPLACE EXCEPT a SPECIAL CAR

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

Answers (2)

Toto
Toto

Reputation: 91430

Just add the dash in the character class:

preg_replace ('#[^.0-9a-z-]+#i', '', $string);
//               here ___^

Upvotes: 0

dave
dave

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

Related Questions