How can I replace white space in filename of uploa

2019-02-22 12:21发布

I'm making a SWF uploader and have my HTML form done.

It works totally fine until I upload a SWF file with spaces in the name.

How can I replace whitespace with underscores?

I have tried...

str_replace(" ","_", $file);

...and...

preg_replace(" ","_", $file);

3条回答
可以哭但决不认输i
2楼-- · 2019-02-22 12:29

How can I replace whitespace with underscores?

The \s character class will match whitespace characters. I've added the + quantifier to collapse multiple whitespace to one _. If you don't want that, remove the +.

$file = preg_replace('/\s+/', '_', $file);
查看更多
Viruses.
3楼-- · 2019-02-22 12:33

I usually approach it from the other side and only allow characters from a white-list; I replace everything except these characters:

$file = preg_replace("/[^-_a-z0-9]+/i", "_", $file);
查看更多
霸刀☆藐视天下
4楼-- · 2019-02-22 12:55

the function is correct but you have to assign it to a variable.

$filename = str_replace/preg_replace(" ","_", $file);
查看更多
登录 后发表回答