我想給字符串與通配符*
匹配的可能性。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