Correctly remove slashes in a regular expression

I need help letting you strip slashes in a pattern that looks for a URL path. I am trying to check if the path contains any numbers in the path after /orders/ as follows:

 $str = '/admin/store/orders/20284?width...'; if ( preg_match ( '/orders/([0-9]+)/', $str, $matches ) ) { print_r($matches); } 

However, I cannot escape the slash. Can anyone help? Thanks.

+5
source share
1 answer

Resetting is done using backslashes ( \/ ). But the slash character for delimiting regular expressions can be any character:

 if ( preg_match ( '~orders/([0-9]+)~', $str, $matches ) ) 

Will work without shielding.

+4
source

Source: https://habr.com/ru/post/1236982/


All Articles