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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With