ben_eire
ben_eire

Reputation: 35

Removing non alphanumeric character with some exceptions

I've came across this function that will do the first part I think

preg_replace("/[^A-Za-z0-9 ]/", '', $string);

But I don't want to remove '-','+' or '|'. How can I make exceptions for these.

Upvotes: 0

Views: 105

Answers (4)

Expedito
Expedito

Reputation: 7795

preg_replace("/[^A-Za-z0-9 +|-]/", '', $string);

Upvotes: 4

Bora
Bora

Reputation: 10717

Add with \char

preg_replace("/[^A-Za-z0-9 \-\+\|]/", '', $string);

Demo: http://regexr.com?35lre

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

You can also use the short form with \w

preg_replace("/[^\w\+\|\-\s]/", '', $string);

Upvotes: 2

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this :

$string  = "abcdAbcd-0999345@dfsdf%+";
echo preg_replace("/[^A-Za-z0-9\+\-\| ]/", '', $string);

Upvotes: 0

Related Questions