Jump to content

String Comparison -- How to return similar strings?


kjtocool

Recommended Posts

I want the user to be able to enter a string, say: "Dog's Play"

 

I have a database filled with various strings like:

 

Dog at Play

Monkey Town

Dog in Play

Doggies Play

John and Sally

Playground

Amy T

Dog World

 

 

What is the best way to utilize PHP and MySQL to return partial and related strings from the database while excluding the irrelevant?

Link to comment
Share on other sites

Use the LIKE statement

$sql="SELECT * FROM table_name WHERE filed_name LIKE '%$string%'";

 

If you want to sort the results in terms of relevance to the search string, then have a look at http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html (see for the one Posted by Brad Satoris on December 13 2004 10:14pm in the middle of that page)

Link to comment
Share on other sites

Thanks, did a little testing, seems like the LIKE statement will help a lot, but it seems to only do a test to see if the items in a specific column contain the string given.  For example, if the column in the database has "Prom" and you do a LIKE search for porm, you will get no results.

 

Is there a simple way to also test for minor misspelling errors like the above?

Link to comment
Share on other sites

No. the 'LIKE' operater is just about the best the MySQL can offer.

 

If you know how to generate a list of different misspelliings you could list them all in the query like this:

$sql="SELECT * FROM table_name WHERE field_name LIKE '%$string%' OR " .
"field_name LIKE '%$misspelling1%' OR " .
"field_name LIKE '%$misspelling2%' OR " .
"field_name LIKE '%$misspelling3%' OR " .
"field_name LIKE '%$misspelling4%'  ";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.