Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Path String With Wildcard in PHP

Howto to compare this string :

commodity/search/oil/branch/index

with this :

commodity/search/*/branch/index

It should return true although "oil" is replace with other word.

like image 991
Azri Jamil Avatar asked Sep 15 '25 07:09

Azri Jamil


1 Answers

$match = preg_match("/commodity\/search\/(.*)\/branch\/index/", "commodity/search/someotherword/branch/index");

$match will be true (or some value evaluating to true, like 1) if a match is found.

Note: the above will match on any extra path, such as commodity/search/some/other/word/branch/index

If you just want to match a single word, but not something resembling a path structure, then you might try something like this:

$match = preg_match("/commodity\/search\/[A-Za-z0-9_-]+\/branch\/index/", "commodity/search/some-OTHER_word/branch/index");

This will only match against upper and lower case a-z characters, numbers, hyphens and underscores. Adjust as needed.

like image 78
Rhys Avatar answered Sep 17 '25 21:09

Rhys