php string matching with wildcard *?

后端 未结 6 1005
悲&欢浪女
悲&欢浪女 2021-02-06 23:18

I want to give the possibility to match string with wildcard *.

Example

$mystring = \'dir/folder1/file\';
$pattern = \'dir/*/file\';

string         


        
6条回答
  •  孤街浪徒
    2021-02-06 23:26

    There is no need for preg_match here. PHP has a wildcard comparison function, specifically made for such cases:

    fnmatch()

    And fnmatch('dir/*/file', 'dir/folder1/file') would likely already work for you. But beware that the * wildcard would likewise add further slashes, like preg_match would.

提交回复
热议问题