I have a regular expression that strips everything but letters. numbers, and periods. How do I also add foreslashes to it?
$targetFile = preg_replace('/[^A-Za-z0-9-.]/', '', $targetFileDirty);
You can escape the foreslash by putting a backslash before it - $targetFile = preg_replace('/[^A-Za-z0-9-.\/]/', '', $targetFileDirty);
Alternatively, and perhaps better, you can use different delimiters instead, e.g. $targetFile = preg_replace('#[^A-Za-z0-9-./]#', '', $targetFileDirty);
To be unicode compatible you can use:
$targetFile = preg_replace('#[^\pL\pN./-]+#', '', $targetFileDirty);
Simply add an escaped slash: [^A-Za-z0-9-.\\/]
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