使用 PHP 从类似于 SQL LIKE '%search%' 的数组中过滤值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5808923/
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
filter values from an array similar to SQL LIKE '%search%' using PHP
提问by Ecropolis
I created an autocomplete field using JQueryUI and I've stored my data in a flat doc. I can read the values into an array... but I would like to be able to return the alphabetic matches based on the user input. So if the array contains [orange,blue,green,red,pink,brown,black]
and the user types bl then I only return [blue,black]
.
我使用 JQueryUI 创建了一个自动完成字段,并将我的数据存储在一个平面文档中。我可以将值读入数组...但我希望能够根据用户输入返回字母匹配。因此,如果数组包含[orange,blue,green,red,pink,brown,black]
并且用户键入 bl 那么我只返回[blue,black]
.
Looking at array_diff()
but without complete matches on the entire value of the array, I'm not sure how to use it... maybe a regular expression thrown in? My two weakest skills array manipulation and regex Thanks for the help!
查看array_diff()
但没有完整匹配数组的整个值,我不知道如何使用它......也许是一个正则表达式?我最弱的两个技能数组操作和正则表达式感谢您的帮助!
回答by Alix Axel
You don't need to use array_filter
and a custom / lambda function, preg_grep
does the trick:
您不需要使用array_filter
自定义/ lambda 函数,即可解决preg_grep
问题:
$input = preg_quote('bl', '~'); // don't forget to quote input string!
$data = array('orange', 'blue', 'green', 'red', 'pink', 'brown', 'black');
$result = preg_grep('~' . $input . '~', $data);
回答by Pascal MARTIN
array_filter()
, with a callback filtering function based on stripos()
, should do the trick.
array_filter()
,使用基于 的回调过滤函数stripos()
,应该可以解决问题。
For example, if the input is stored in $input
, and your array in $data
:
例如,如果输入存储在 中$input
,而您的数组存储在$data
:
$input = 'bl';
$data = array('orange', 'blue', 'green', 'red', 'pink', 'brown', 'black');
Filtering to keep only the words that contain $input
(no matter where in the string)could be done this way :
过滤以仅保留包含的单词$input
(无论字符串中的哪个位置)可以通过以下方式完成:
$result = array_filter($data, function ($item) use ($input) {
if (stripos($item, $input) !== false) {
return true;
}
return false;
});
var_dump($result);
And, here, you'd get :
而且,在这里,你会得到:
array
1 => string 'blue' (length=4)
6 => string 'black' (length=5)
Changing the filtering callback, you can :
更改过滤回调,您可以:
- Test if the string begins with the input -- testing if the value returned by
stripos()
is=== 0
- Use a case-sensitive matching function, like
strpos()
- 测试字符串是否以输入开头——测试返回的值
stripos()
是否为=== 0
- 使用区分大小写的匹配函数,例如
strpos()
回答by Michael
You could simply iterate through the array to look for all strings that start with the given letters. Having the list of words already sorted in your flat file would probably speed things up. I think this works pretty well:
您可以简单地遍历数组以查找以给定字母开头的所有字符串。在您的平面文件中已经排序的单词列表可能会加快速度。我认为这很有效:
$input = strtolower("bl"); //from the user
$colors = array("black", "blue", "brown", "..."); //already sorted alphabetically
$matches = array();
foreach ($colors as $c){
if (strpos($c, $input) === 0){
//if $c starts with $input, add to matches list
$matches[] = $c;
} else if (strcmp($input, $c) < 0){
//$input comes after $c in alpha order
//since $colors is sorted, we know that we won't find any more matches
break;
}
}