php 字符串匹配与通配符 *?
2022-08-30 13:08:37
我想提供将字符串与通配符匹配的可能性。*
例
$mystring = 'dir/folder1/file';
$pattern = 'dir/*/file';
stringMatchWithWildcard($mystring,$pattern); //> Returns true
示例 2:
$mystring = 'string bl#abla;y';
$pattern = 'string*y';
stringMatchWithWildcard($mystring,$pattern); //> Returns true
我的想法是这样的:
function stringMatch($source,$pattern) {
$pattern = preg_quote($pattern,'/');
$pattern = str_replace( '\*' , '.*?', $pattern); //> This is the important replace
return (bool)preg_match( '/^' . $pattern . '$/i' , $source );
}
基本上替换到(考虑在环境中匹配字符串)©vbence*
.*?
*nix
*
empty
任何改进/建议?
之所以添加,是因为preg_match返回 intreturn (bool)