如何替换上传文件的文件名中的空格

问题描述:

我正在制作一个 SWF 上传器并完成我的 HTML 表单.

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

在我上传名称中包含空格的 SWF 文件之前,它完全正常.

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

如何用下划线替换空格?

How can I replace whitespace with underscores?

我试过了...

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

……还有……

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

如何用下划线替换空格?

How can I replace whitespace with underscores?

\s 字符类将匹配空白字符.我添加了 + 量词以将多个空格折叠为一个 _.如果您不希望那样,请删除 +.

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