Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match all characters except letters and numbers

Tags:

regex

php

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.

like image 384
Timay Avatar asked Feb 04 '23 06:02

Timay


2 Answers

$newfilename=preg_replace('/[^a-zA-Z0-9.]/','',$filename);
like image 194
YOU Avatar answered Feb 06 '23 10:02

YOU


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);
like image 40
kennytm Avatar answered Feb 06 '23 11:02

kennytm