Regex matches all characters except letters and numbers.

I want to clear the file names of all downloaded files. I want to delete all characters except periods, letters and numbers. I am not very familiar with regex, so I decided to ask here. Can someone point me to a useful site or show me how to do this? I am using PHP.

+6
php regex
source share
3 answers
$newfilename=preg_replace('/[^a-zA-Z0-9.]/','',$filename); 
+8
source share
 s/[^.a-zA-Z\d]//g 

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

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

Try using this:

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

It will delete everything except letters and numbers.

0
source share

All Articles