Solved

String comparison to give suggestions for misspelled words?

Posted on 2006-07-13
3
601 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

792 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