在php中模糊搜索数组
在我搜索之后,我发现如何对字符串进行模糊搜索
,但我有一个字符串数组
$search = {"a" =>; “笔记本电脑”,“b”=> “屏幕” ....}
我从数据库 MySQL 检索到的
是否有任何 php 类或函数可以对单词数组进行模糊搜索
,或者至少有一个可能包含一些有用信息的链接,
我看到了一条评论推荐使用 PostgreSQL
和它的模糊搜索功能,但
公司已经有了 MySQL DB
有什么推荐吗?
after i searched i found how to do a fuzzy searching on a string
but i have an array of strings
$search = {"a" => "laptop","b" => "screen" ....}
that i retrieved from the DB MySQL
IS there any php class or function that does fuzzy searching on an array of words
or at least a link with maybe some useful info's
i saw a comment that recommend using PostgreSQL
and it's fuzzy searching capability but
the company had already a MySQL DB
Is there any recommendation ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 MySQL 中执行此操作,因为您已经拥有 MySQL 数据库 - 如何对 MYSQL 中的公司名称与 PHP 进行模糊匹配auto-complete? 其中提到了 MySQL Double Metaphone 实现 并在 SQL for MySQL 中有实现 5.0+
编辑:很抱歉在这里回答,因为评论内容超出了…
因为您已经使用 PHP Levenshtein 函数 那么我建议您首先尝试该方法。软件是迭代的; PHP 数组搜索可能正是您想要的,但您必须首先根据您的要求测试和实现它。正如我在 你的其他问题键入时查找解决方案可能是这里最简单的解决方案,它只是在用户键入时缩小产品范围。可能不需要实现任何模糊搜索,因为您使用用户自己进行模糊搜索:-)
例如,用户开始输入
S
、a
、m
可让您将产品缩小到以Sam
开头的产品。因此,您始终只让用户选择您已知有效的产品。You could do this in MySQL since you already have a MySQL database - How do I do a fuzzy match of company names in MYSQL with PHP for auto-complete? which mentions the MySQL Double Metaphone implementation and has an implementation in SQL for MySQL 5.0+
Edit: Sorry answering here as there is more than could fit in a comment…
Since you've already accepted an answer using PHP Levenshtein function then I suggest you try that approach first. Software is iterative; the PHP array search may be exactly what you want but you have to test and implement it first against your requirements. As I said in your other question a find as you type solution might be the simplest solution here, which simply narrows the product as the user types. There might not be a need to implement any fuzzy searching since you are using the User to do the fuzzy search themselves :-)
For example a user starts typing
S
,a
,m
which allows you to narrow the products to those beginning withSam
. So you are always only letting the user select a product you already know is valid.查看 Levenshtein 函数
基本上它会给你带来差异(在成本方面)到字符串之间。即,将字符串 A 转换为字符串 B 的成本是多少。
为自己设置一个阈值 levenshein 距离,两个单词低于该阈值的任何值都意味着它们是相似的。
另外 Bitap 算法 速度更快,因为它可以通过按位运算符实现,但我相信你会自己实现它,除非某个地方有一个 PHP 库。
编辑
要使用 levenshtein 方法:
搜索字符串是“maptop”,并且您将“成本阈值”设置为 2。这意味着您希望任何距离搜索字符串进行两次字符串转换操作的单词。
所以你循环遍历字符串数组“A”,直到
levenshtein ( A[i] , searchString ) <= 2
这将是你的匹配。 但是您可能会得到多个匹配的单词,因此这取决于您想要如何处理额外的结果。
Look at the Levenshtein function
Basically it gives you the difference (in terms of cost) between to strings. I.e. what is the cost to transform string A into string B.
Set yourself a threshold levenshein distance and anything under that for two words mean they're similar.
Also the Bitap algorithm is faster since it can be implemented via bitwise operators, but I believe you will have to implement it yourself, unless there is a PHP lib for it somewhere.
EDIT
To use levenshtein method:
The search string is "maptop", and you set your "cost threshold" to say 2. That means you want any words that are two string transform operations away from your search string.
so you loop through your array "A" of strings until
levenshtein ( A[i] , searchString ) <= 2
That will be your match. However you may get more than one word that matches, so it is up to you how you want to handle the extra results.