我想给字符串与通配符*
匹配的可能性。php字符串匹配通配符*?
例
$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);
}
基本上替换*
到.*?
(考虑*nix
环境*
匹配empty
字符串)©vbence
任何改进/建议?
//添加return (bool)
因为的preg_match返回int
嗨马里奥。 fnmatches还有一些其他功能,如'[]'。我只需要'*'作为“特殊字符” – dynamic 2011-05-29 16:11:36
fnmatch在非POSIX系统上不受支持。 (我发现php5.3 +现在也支持windows)。所以这并不总是最好的方法 – Pinoniq 2014-05-21 14:49:58