I need help escaping slashes in a pattern which searches a URL path. I'm trying to check if a path contains any numbers in the path after /orders/
like this:
$str = '/admin/store/orders/20284?width...';
if ( preg_match ( '/orders/([0-9]+)/', $str, $matches ) )
{
print_r($matches);
}
However, I am not able to escape the slashes properly. Can anybody help? Thank you.
Escaping is done with backslashes (\/
). But the slash character to delimit regular expressions can be any character:
if ( preg_match ( '~orders/([0-9]+)~', $str, $matches ) )
Will work without escaping.
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