如何在 PHP 中为拼写错误的单词找到相似的单词? [英] How to find a similar word for a misspelled one in PHP?

查看:38
本文介绍了如何在 PHP 中为拼写错误的单词找到相似的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会解释我的问题:

我有一个名为 country 的数据库表.它有两列:IDname.

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 中为拼写错误的单词找到相似的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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