开发者

Regex to match all characters except letters and numbers

开发者 https://www.devze.com 2022-12-18 08:21 出处:网络
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 开发者_JAVA技巧with regex so I thought I would ask here.

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 开发者_JAVA技巧with regex so I thought I would ask here.

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


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


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);


Try to use this:

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

It will remove all but letters and numbers.

0

精彩评论

暂无评论...
验证码 换一张
取 消