Solved

String comparison to give suggestions for misspelled words?

Posted on 2006-07-13
3
596 Views
Last Modified: 2009-12-16
Hi,
I've got a form where someone can enter in a name of something. When submitted the name searched in the database and if found it returns the data for that name. What I want is to give a suggestion when a name is entered that is not in my database. Sort of like what Dictionary.com and Google do when you search for a misspelled word?

Anyone know how to do this?
0
Comment
Question by:Darksbane
3 Comments
 
LVL 7

Expert Comment

by:JB04
ID: 17103871
You can use an SQL query to find similar words like so:-


            $sql = "
            SELECT
                  *
            FROM
                  sometable
            WHERE
                  name LIKE ('%something%')
            ";


PHP also has some functions to show similar sounding words which can be used for things like this, check out the following functions:-

http://php.net/soundex
http://php.net/metaphone


0
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 100 total points
ID: 17103935
You can also use the mysql soundex function;
soundex is an algorithm that goes through the letters and tries to group on similar sounds from the patterns of the words.

You could do

select * from tablename where left(soundex(name), 4) like concat(left(soundex('EnteredValue'), 4), '%')

Which would give you everything that is like the first couple of letters in the name.
0
 
LVL 1

Accepted Solution

by:
cholin earned 150 total points
ID: 17104067
Well, if you want sorta what Google does, there's a library for PHP called pSpell (www.php.net/pspell) which allows you to set specific dictionaries based on the aSpell engine.  If you make a custom dictionary that is basically all the usernames from your database, you can use the pSpell functions to suggest the closest matches with minimal code.

So basically every time a user signs up for your site, just add their username to the dictionary, and then whenever people search for a username, you can check for a match, or if there isnt one, you can suggest the closest matches.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now