Timay
Timay

Reputation: 145

Regex to match all characters except letters and numbers

I want to clean the filenames of all uploaded files. I want to remove all characters except periods, letters and numbers. I'm not good with regex so I thought I would ask here.

Can someone show me how to put this together? I'm using PHP.

Upvotes: 11

Views: 28719

Answers (3)

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9295

Try to use this:

$cleanString = preg_replace('#\W#', '', $string);

It will remove all but letters and numbers.

Upvotes: 0

kennytm
kennytm

Reputation: 523304

s/[^.a-zA-Z\d]//g

(This is a Perl expression of how to use the RegExp. In PHP you do:

$output = preg_replace('/[^.a-zA-Z\d]/', '', $input);

Upvotes: 1

YOU
YOU

Reputation: 123831

$newfilename=preg_replace('/[^a-zA-Z0-9.]/','',$filename);

Upvotes: 13

Related Questions