从使用PHP类似于SQL LIKE'%搜索%'的数组筛选值 [英] filter values from an array similar to SQL LIKE '%search%' using PHP

查看:251
本文介绍了从使用PHP类似于SQL LIKE'%搜索%'的数组筛选值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用jQueryUI的自动填充字段,我已经存储在平面文档我的数据。我可以读取值到一个数组...但我希望能够返回基于用户输入的字母匹配。因此,如果该数组包含 [橙色,蓝色,绿色,红色,粉色,棕色,黑色] 和用户类型BL当时我只返回 [蓝色黑]

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].

看着和array_diff(),但没有对阵列的整个价值完整的比赛,我不知道如何使用它...也许一个普通的前pression扔?我的两个最弱的技能阵列处理和正则表达式感谢您的帮助!

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_filter 和一个自定义/λ功能的 preg_grep 的伎俩:

You don't need to use array_filter and a custom / lambda function, preg_grep does the trick:

$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);

这篇关于从使用PHP类似于SQL LIKE'%搜索%'的数组筛选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆