问题描述
我会解释我的问题:
我有一个名为 country
的数据库表.它有两列:ID
和 name
.
I have a database table called country
. It has two columns: ID
and name
.
当我想搜索 'paris'
,但拼错了这个词时:'pares'
('e'
而不是 >'i'
),我不会从 DB 得到任何结果.
When I want to search for 'paris'
, but misspelled the word: 'pares'
('e'
instead of 'i'
), I won't get any result from DB.
我希望系统建议可以帮助搜索的相似词.
I want the the system to suggest similar words that could help in the search.
因此,我正在寻求帮助编写脚本,该脚本从数据库中提出建议,其中包含类似的词,例如:paris、paredes 等.
So, I am looking for help writing a script that makes suggestions from the DB that contain similar words like: paris, paredes, ... etc.
推荐答案
在 PHP 中你应该使用 metaphone
它比 soundex
.
In PHP you should use metaphone
it is more accurate than soundex
.
但是您的问题是从数据库中获取数据.你没有提到数据库.在 MySQL 中,您可以使用 SOUNDEX
函数.您只需要更改查询中的 where 子句
But your problem is getting the data from the database. You've not mentioned the DB. In MySQL you can make use of the SOUNDEX
function. You just need to change your where clause in the query from
...where city = '$input_city'
到
... where soundex(city) = soundex('$input_city')
或者更好的是你可以使用 听起来像
操作符为
or even better you can use SOUNDS LIKE
operator as
... where city sounds like '$input_city'
这篇关于如何在 PHP 中为拼写错误的单词找到相似的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!