Solved

String comparison to give suggestions for misspelled words?

Posted on 2006-07-13
3
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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 …

730 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