我想提供将字符串与通配符匹配的可能性*
.
Example
$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 );
}
基本取代*
to .*?
(考虑到在*nix
环境*
火柴empty
细绳)©vbence
有任何改进/建议吗?
// 添加return (bool)
因为 preg_match 返回 int
没有必要preg_match
这里。 PHP 有一个通配符比较函数,专门针对这种情况:
fnmatch() http://php.net/fnmatch
And fnmatch('dir/*/file', 'dir/folder1/file')
可能已经适合你了。但要注意的是*
通配符同样会添加更多斜杠,就像 preg_match 一样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)