Click here to Skip to main content
15,881,757 members
Articles / Database Development / MySQL

Searching for words that sound like..

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
10 Jun 2009CPOL 17.8K   15   1
Combining methods to better search for words that sound like other words when searching a MySQL database.

Introduction

The PHP function similar_text and other variants are useful when looking for similar words. When used in conjunction with MySQL, additional programming is necessary. The MySQL function soundex is not sufficient.

Background

Recently, I needed to query a database and compare the returned results to a search term. If the fields in the table contained one word, this would be easy, but the fields being searched included many words as part of a name usually separated by spaces but sometimes with a comma character.

Using the code

This brief code snippet includes the main part that tokenized the returned results from the database, and compares each term using the similar_text function. The code needs to be expanded to search for several words at a time.

PHP
$trimmed = "word_to_search"; // we only search for one word

$dblink = mysql_connect("localhost", "%user%", "%password%")  
 or die("connection was unsuccessful");
mysql_select_db("db_name", $dblink) 
 or die("MYSQL Database Error: " . mysql_error());

//We're only selecting one field 
$query = mysql_query("SELECT field_name FROM TABLE");

while($r = mysql_fetch_array($query))
{
    //parse the multi-word string returned
    $tok = strtok($r["field_name"], ", ");
    while($tok !== false)
    {
        //needed to strip a quote character that enclosed the string inside the field 
        similar_text(strtoupper($trimmed), trim(strtoupper($tok),'"'), $percentage);
        $percentage = number_format($percentage, 0);
        if($percentage >= 80) //define a threshold, in this case an 80 percent match
        {
            //prints the results to clearly indicated which terms meet the threshold
            $results_record["matched on:<font color=\"#ff0000\"> " .
              trim(strtoupper($tok),'"') . 
              "</font> in string: <font color=\"#0000CC\"> " . 
              $r[field_name] . "</font>"]  = $percentage;
        }
        $tok = strtok(", ");
    } 
}

if (!$results_record)
{
    echo "nothing found";
}
else
{
    asort($results_record, SORT_NUMERIC);
    //echo key pair values
    foreach($results_record as $result => $percentage)
    {
        echo $result . " - " . $percentage."%<BR>" ;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
low cost, robust, and simple IT solutions

Comments and Discussions

 
GeneralGood Job Pin
adatapost10-Jun-09 20:30
adatapost10-Jun-09 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.