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\/:You can make your regex shorter by using
\win place of[a-zA-Z0-9_], also you can avoid escaping the/by using a different delimiter say~: