Removing spaces and anything that is not alphanumeric
-
25-09-2019 - |
문제
I'm trying to remove everything that is not alphanumeric, or is a space with _:
$filename = preg_replace("([^a-zA-Z0-9]|^\s)", "_", $filename);
What am I doing wrong here, it doesn't seem to work. I've tried several regex combinations...(and I'm generally not very bright).
해결책
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.
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);
제휴하지 않습니다 StackOverflow