ass*_*qaf 15 php string similarity string-comparison
我会解释我的问题:
我有一个名为的数据库表country.它有两列:ID和name.
当我想要搜索'paris',但拼错字:'pares'('e'代替'i'),我不会得到任何DB结果.
我希望系统能够提出可能有助于搜索的类似单词.
因此,我正在寻找帮助编写一个脚本,该脚本从DB中提出包含类似词语的建议:paris,paredes,...等.
cod*_*ict 20
在PHP中你应该使用metaphone它比它更准确soundex.
但是你的问题是从数据库中获取数据.你没有提到过DB.在MySQL中,您可以使用该SOUNDEX功能.您只需要在查询中更改where子句
...where city = '$input_city'
Run Code Online (Sandbox Code Playgroud)
至
... where soundex(city) = soundex('$input_city')
Run Code Online (Sandbox Code Playgroud)
甚至更好,你可以使用SOUNDS LIKE运营商
... where city sounds like '$input_city'
Run Code Online (Sandbox Code Playgroud)
soundex将返回表示其声音的单词的数字代码.听起来相似的单词将具有相同的soundex代码.您可以拥有一个包含单词及其soundex代码的表格,您可以使用它来查找类似的发音单词.然后你可以使用他们的levenshtein距离对它们进行排序.
如果您正在寻找更简单的东西而且您只想在数据库查询中处理拼写错误,那么您可以这样做
select * from country where city SOUNDS LIKE 'Paris' 代替 select * from country where city='Paris'