How to find a similar word for a misspelled one in PHP?

assaqqaf picture assaqqaf · Oct 15, 2010 · Viewed 7.1k times · Source

I'll explain my problem:

I have a database table called country. It has two columns: ID and name.

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.

So, I am looking for help writing a script that makes suggestions from the DB that contain similar words like: paris, paredes, ... etc.

Answer

codaddict picture codaddict · Oct 15, 2010

In PHP you should use metaphone it is more accurate than soundex.

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'

to

... where soundex(city) = soundex('$input_city')

or even better you can use SOUNDS LIKE operator as

... where city sounds like '$input_city'