I have an array the elements of which are paths and contain forward slashes and exclamation marks.
And I need to inject such an array as regex pattern to preg_match()
$url = 'example.com/path/to/!another';
$arr = ['path/to/!page', 'path/to/!another'];
$unescapedPattern = implode('|', $arr);
$escapedPattern = preg_quote($unescapedPattern, '/');
if(preg_match('/('.$escapedPattern.')/', $url)) {
echo 'The input url contains one of the paths in array!';
}
Of course the pattern is not applied because preg_quote()
escapes the vertical bar
How to exclude it from escaping or how to resolve the issue another way?