[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String comparison to give suggestions for misspelled words?

Posted on 2006-07-13
3
Medium Priority
?
619 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 400 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 600 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 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