Removing spaces and anything that is not alphanumeric

家住魔仙堡 提交于 2019-12-01 17:18:13

Try this:

$filename = preg_replace("/[^a-zA-Z0-9 ]/", "_", $filename);
$filename = preg_replace('~[\W\s]~', '_', $filename);

If I understand your question correctly, you want to replace any space (\s) or non-alphanumerical (\W) character with a '_'. This should do fine. Note the \W is uppercase, as opposed to lowercase \w which would match alphanumerical characters.

Knickerless-Noggins

The solution that works for me is:

$filename = preg_replace('/\W+/', '_', $filename);

The + matches blocks of one or more occurances of \W whitespace which includes spaces and all non-alphanumeric characters

Try

$filename = preg_replace("/[a-zA-Z0-9]|\s/", "_", $filename);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!