I can't figure out how to change my regex below to keep the slashes. I want to make sure it only contains letters, numbers, underscores, dashes AND slashes.
($query is something like e.g. /offer/some-offer-bla-bla-bla)
$query = preg_replace('/[^-a-zA-Z0-9_]/', '', $query);
Thanks
Just include the / in the character class. But since you are using / as the regex delimiter you need to escape it aswell as \/:
$query = preg_replace('/[^-a-zA-Z0-9_\/]/', '', $query);
^^
You can make your regex shorter by using \w in place of [a-zA-Z0-9_], also you can avoid escaping the / by using a different delimiter say ~:
$query = preg_replace('~[^-\w/]~', '', $query);
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