Stephane Grenier
Stephane Grenier

Reputation: 15925

PHP Regex - Remove invalid characters from string

I know there are many regex questions, but I don't know it well enough, nor could I find an answer explaining it well enough to derive the solution I'm trying to get.

In PHP, using

preg_replace($pattern,"",$data); 

I'd like to allow all alphanumeric characters along with the minus, period, and underscore characters. What is the $pattern I need?

Upvotes: 1

Views: 4486

Answers (3)

Edwin Thomas
Edwin Thomas

Reputation: 1186

Try this, i know this is not Regex...

$answer=iconv("UTF-8", "ISO-8859-1//IGNORE", $data);

Upvotes: 1

T.Todua
T.Todua

Reputation: 56497

maybe this can be tried:

$cleaner_input = trim(strip_tags($text));

Upvotes: -1

Explosion Pills
Explosion Pills

Reputation: 191789

$pattern = "/[^\w.-]/";

This is the negated character class of exactly the requirements you described.

Upvotes: 7

Related Questions