Php Search_Array 使用通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5592120/
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 Search_Array using Wildcard
提问by PROFESSOR
i m trying to search an array which contains patterns like
我试图搜索一个包含类似模式的数组
mike_45
peter_23
jim_12
and wants to search the particular pattern someway like
并希望以某种方式搜索特定模式
array_search('mike*',$array);
can somebody pls suggest me a useful way to do this
有人可以建议我一个有用的方法来做到这一点
thanks in advance...
提前致谢...
回答by KingCrunch
With array_search()
I dont see, that this is possible. I would array_filter()give a try. Also look at fnmatch(). Untested:
随着array_search()
我不明白,这是可能的。我会array_filter()试一试。也看看fnmatch()。未经测试:
$pattern = 'mike*';
$array = array('mike_45','peter_23','jim_12');
$array = array_filter($array, function($entry) use ($pattern) {
return fnmatch($pattern, $entry);
});
Requires PHP5.3 and also see my comment on hsz's answer. Except that this one dont need to rewrite the search pattern, its similar.
需要 PHP5.3 并查看我对 hsz 回答的评论。除了这个不需要重写搜索模式,它的类似。
回答by WildOne
Another way to wildcard search is to turn the array back into a string variable and then use strpos function. This will provide a true/false logical on if the string can match any array entry.
通配符搜索的另一种方法是将数组转回字符串变量,然后使用 strpos 函数。如果字符串可以匹配任何数组条目,这将提供真/假逻辑。
strpos(implode($array), 'mike');
This can be used in the following way.
这可以按以下方式使用。
if(strpos(implode($array), "mike"))
Perform actions here