Mike
Mike

Reputation:

PHP RegExp certain chars

What would be a good way to get rid of all chars except, letters, numbers, double quotes and hyphens. This is what I got so far.

$test = preg_replace('#[^a-zA-Z0-9"-]#',' ',$string); 

Any suggestions?

Thanks

Upvotes: 1

Views: 106

Answers (2)

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

You can use \d to match digits, and the flag i to match a-z with case insensitivity.

$test = preg_replace('#[^a-z\d\w"-]#i','',$string);

Here is the php regex-syntax reference: http://se.php.net/manual/en/regexp.reference.php

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351476

Your regex is about as good a solution as you are going to find.

Upvotes: 3

Related Questions