php 字符串匹配通配符 *?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6163055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php string matching with wildcard *?
提问by dynamic
I want to give the possibility to match string with wildcard *.
我想提供将 string 与通配符匹配的可能性*。
Example
例子
$mystring = 'dir/folder1/file';
$pattern = 'dir/*/file';
stringMatchWithWildcard($mystring,$pattern); //> Returns true
Example 2:
示例 2:
$mystring = 'string bl#abla;y';
$pattern = 'string*y';
stringMatchWithWildcard($mystring,$pattern); //> Returns true
I thought something like:
我想的是这样的:
function stringMatch($source,$pattern) {
$pattern = preg_quote($pattern,'/');
$pattern = str_replace( '\*' , '.*?', $pattern); //> This is the important replace
return (bool)preg_match( '/^' . $pattern . '$/i' , $source );
}
Basically replacing *to .*?(considering in *nixenvironment *matches emptystring) ?vbence
基本上替换*为.*?(在*nix环境*中考虑匹配empty字符串) ?vbence
Any improvments/suggests?
任何改进/建议?
// Added return (bool)because preg_match returns int
// 添加return (bool)是因为 preg_match 返回 int
回答by mario
There is no need for preg_matchhere. PHP has a wildcard comparison function, specifically made for such cases:
这里没有必要preg_match。PHP 有一个通配符比较功能,专门针对这种情况:
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.
并且fnmatch('dir/*/file', 'dir/folder1/file')可能已经为您工作。但请注意,*通配符同样会添加更多斜杠,就像 preg_match 一样。
回答by Dogbert
You should just use .*instead.
你应该用它.*来代替。
$pattern = str_replace( '*' , '.*', $pattern); //> This is the important replace
Edit: Also your ^and $were in the wrong order.
编辑:还有你的^和$顺序错误。
<?php
function stringMatchWithWildcard($source,$pattern) {
$pattern = preg_quote($pattern,'/');
$pattern = str_replace( '\*' , '.*', $pattern);
return preg_match( '/^' . $pattern . '$/i' , $source );
}
$mystring = 'dir/folder1/file';
$pattern = 'dir/*/file';
echo stringMatchWithWildcard($mystring,$pattern);
$mystring = 'string bl#abla;y';
$pattern = 'string*y';
echo stringMatchWithWildcard($mystring,$pattern);
Working demo: http://www.ideone.com/mGqp2
工作演示:http: //www.ideone.com/mGqp2
回答by vbence
.+?
Causes non-greedy matching for all characters. This is NOT equal to "*" becuase it will not match the empty string.
导致所有字符的非贪婪匹配。这不等于“*”,因为它不会匹配空字符串。
The following pattern will match the empty string too:
以下模式也将匹配空字符串:
.*?
so...
所以...
stringMatchWithWildcard ("hello", "hel*lo"); // will give true
回答by netcoder
You're mixing up ending ($) and beginning (^). This:
你把结尾 ( $) 和开头 ( ^)搞混了。这个:
preg_match( '/$' . $pattern . '^/i' , $source );
Should be:
应该:
preg_match( '/^' . $pattern . '$/i' , $source );
回答by Spudley
The one problem you'll have is that the call to preg_quote()will escape the asterisk character. Given that, your str_replace()will replace the *, but not the escape character in front of it.
您将遇到的一个问题是调用preg_quote()将转义星号字符。鉴于此,您str_replace()将替换*, 但不会替换它前面的转义字符。
Therefore you should change the str_replace('*' ..)with str_replace('\*'..)
因此你应该改变str_replace('*' ..)与str_replace('\*'..)
回答by Tim13
$pattern = str_replace( '\*' , '.+?', $pattern); // at least one character

