I have an array of paths that consist of regex strings:
$paths = [
'/cars$',
'.*\/cars$',
'^cars\/.*/',
'/trucks$',
'.*\/trucks$',
];
I want to check if my current path matches something in that array, eg:
if (in_array($current_path, $paths)) {
//Do something
}
So for example, if my URL is some-site.com/cars
or some-site.com/cars/honda
then I want to return something in the if statement.
But I cannot figure out how to get this working with the regex.
I investigated using preg_grep()
but that seems to only work with one set of regex rules at a time.
I can get this to work with preg_match
and a string, eg:
$paths = '/cars$|.*\/cars$|^cars\/.*/|/trucks$|.*\/trucks$';
if (preg_match($paths, $current_path)) {
//Do something
}
But I will eventually have many URLs added to this list so I would prefer to use an array.
Would anyone know if there is a function that achieves this?