Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 466869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:31:25+00:00 2026-05-12T23:31:25+00:00

Quick MYSQL/PHP question. I’m using a not-so-strict search query as a fallback if no

  • 0

Quick MYSQL/PHP question. I’m using a “not-so-strict” search query as a fallback if no results are found with a normal search query, to the tune of:

foreach($find_array as $word) { 
  clauses[] = "(firstname SOUNDS LIKE '$word%' OR lastname SOUNDS LIKE '$word%')";
}
if (!empty($clauses)) $filter='('.implode(' AND ', $clauses).')';
$query = "SELECT * FROM table WHERE $filter";

Now, I’m using PHP to highlight the results, like:

foreach ($find_array as $term_to_highlight){
    foreach ($result as $key => $result_string){
        $result[$key]=highlight_stuff($result_string, $term_to_highlight);
    }
}

But this method falls on its ass when I don’t know what to highlight. Is there any way to find out what the “sound-alike” match is when running that mysql query?

That is to say, if someone searches for “Joan” I want it to highlight “John” instead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:31:26+00:00Added an answer on May 12, 2026 at 11:31 pm

    The SOUND LIKE condition just compares the SOUNDEX key of both words, and you can use the PHP soundex() function to generate the same key.

    So, if you found a matching row and needed to find out which word to highlight, you can fetch both the firstname and lastname, and then use PHP to find which one matches and highlight just that word.

    I made this code just to try this out. (Had to test my theory xD)

    <?php
    // A space seperated string of keywords, presumably from a search box somewhere.
    $search_string = 'John Doe';
    
    // Create a data array to contain the keywords and their matches.
    // Keywords are grouped by their soundex keys.
    $data = array();
    foreach(explode(' ', $search_string) as $_word) {
        $data[soundex($_word)]['keywords'][] = $_word;
    }
    
    // Execute a query to find all rows matching the soundex keys for the words.
    $soundex_list = "'". implode("','", array_keys($data)) ."'";
    $sql = "SELECT id, firstname, lastname
            FROM   sounds_like
            WHERE  SOUNDEX(firstname) IN({$soundex_list})
            OR     SOUNDEX(lastname)  IN({$soundex_list})";
    $sql_result = $dbLink->query($sql);
    
    // Add the matches to their respective soundex key in the data array.
    // This checks which word matched, the first or last name, and tags
    // that word as the match so it can be highlighted later.
    if($sql_result) {
        while($_row = $sql_result->fetch_assoc()) {
            foreach($data as $_soundex => &$_elem) {
                if(soundex($_row['firstname']) == $_soundex) {
                    $_row['matches'] = 'firstname';
                    $_elem['matches'][] = $_row;
                }
                else if(soundex($_row['lastname']) == $_soundex) {
                    $_row['matches'] = 'lastname';
                    $_elem['matches'][] = $_row;
                }
            }
        }
    }
    
    // Print the results as a simple text list.
    header('content-type: text/plain');
    echo "-- Possible results --\n";
    
    foreach($data as $_group) {
        // Print the keywords for this group's soundex key.
        $keyword_list = "'". implode("', '", $_group['keywords']) ."'";
        echo "For keywords: {$keyword_list}\n";
    
        // Print all the matches for this group, if any.
        if(isset($_group['matches']) && count($_group['matches']) > 0) {
            foreach($_group['matches'] as $_match) {
                // Highlight the matching word by encapsulatin it in dashes.
                if($_match['matches'] == 'firstname') {
                    $_match['firstname'] = "-{$_match['firstname']}-";
                }
                else {
                    $_match['lastname'] = "-{$_match['lastname']}-";
                }
    
                echo " #{$_match['id']}: {$_match['firstname']} {$_match['lastname']}\n";
            }
        }
        else {
            echo " No matches.\n";
        }
    }
    ?>
    

    A more generalized function, to pull out the matching soundex word from a strings could look like:

    <?php
    /**
     * Attempts to find the first word in the $heystack that is a soundex
     * match for the $needle.
     */
    function find_soundex_match($heystack, $needle) {
        $words = explode(' ', $heystack);
        $needle_soundex = soundex($needle);
        foreach($words as $_word) {
            if(soundex($_word) == $needle_soundex) {
                return $_word;
            }
        }
        return false;
    }
    ?>
    

    Which, if I am understanding it correctly, could be used in your previously posted code as:

    foreach ($find_array as $term_to_highlight){
        foreach ($result as $key => $result_string){
            $match_to_highlight = find_soundex_match($result_string, $term_to_highlight);
            $result[$key]=highlight_stuff($result_string, $match_to_highlight);
        }
    }
    

    This wouldn’t be as efficient tho, as the more targeted code in the first snippet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a quick csv from a mysql table with a query like: select
Quick question. There is a legacy website (that is not under my control and
Quick question regarding CSS and the browser. I tried searching SO and found some
I'm somewhat new to Javascript, not HTML/CSS or PHP/MySQL, just JS. I have a
Hey guys quick question, I currently have an insert statement $query= INSERT into new_mail
a quick question. I am using the jQuery.forms.js plug-in. I have a form that
Just a quick question. Is there any performance difference between using PDO::fetchAll() and PDO::fetch()
Quick question. What do you think, I have a few sites that use a
Quick question: Would it be a good or a bad idea to implement my
Quick question: what is the compiler flag to allow g++ to spawn multiple instances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.