Results 1 to 8 of 8
  1. #1
    Member
    Join Date
    June 8th, 2005
    Posts
    68
    How to Bold search matches?
    I currently use MySql Match function for my search. How do I highlight/bold the words that are matches to the query. Like what Google does...(i.e. if I search for Reebok running shoes, how do I highlight the word Reebok in the product name and running in the description?) Thanks.

  2. #2
    ABW Ambassador sjangro's Avatar
    Join Date
    January 18th, 2005
    Location
    Boston
    Posts
    1,529
    Just do a search and replace on the text before you display it. There are lots of ways to do it. In php if you're dealing with several terms, preg_replace works well:
    Code:
    // put the search terms in an array
    // you'll need to do something more than this to get the terms into
    // the array.
    
    $search = array ('word1','word2','word3');
    
    // replace them with bold tags around them
    $replace = array (
    '<b>word1</b>',
    '<b>word2</b>',
    '<b>word3</b>');
    
    $newtext = preg_replace($search, $replace, $text);
    
    echo $newtext;
    (warning: this is off the top of my head, untested, and may have errors, but hopefully you get the idea)

  3. #3
    Member sendmemybill's Avatar
    Join Date
    August 6th, 2005
    Location
    St-Georges, Québec, Canada
    Posts
    105
    <?php

    $string = $text; // put the text of the field that resulted from your mysql in a variable
    $patterns = '/the search string here/'; // the pattern to be changed in bold (you can put a $variable, the one your user searched for in exemple
    $replacements = '<strong>the search string here</strong>'; //will be in bold

    echo preg_replace($patterns, $replacements, $string); // echo the entire text from the search field in database but with the searched string in bold

    ?>

  4. #4
    Newbie
    Join Date
    January 16th, 2006
    Posts
    1
    Will it replace all words even if they appear in the different location. Google does bold all words even if they are not next to each other. OR do we have handle them separately?

  5. #5
    Member sendmemybill's Avatar
    Join Date
    August 6th, 2005
    Location
    St-Georges, Québec, Canada
    Posts
    105
    I did not tested that but logicaly yes, it should put in bold all the replaced word in the string. I would say, test it!
    "Effective people are not problem-minded; they're opportunity minded. They feed opportunities and starve problems."

  6. #6
    Member
    Join Date
    April 11th, 2006
    Location
    Michigan
    Posts
    70
    If you are selecting data from mySQL database then you need to use preg_replace for all the columns that you would like to highlight. It will replace all words even if they appear in the different location.
    Burhan Tanweer
    Marketing Manager
    sales/at/awebhosts.com
    [URL=http://www.awebhosts.com]www.awebhosts.com [/URL]

  7. #7
    Full Member
    Join Date
    January 16th, 2006
    Posts
    447
    Question
    Quote Originally Posted by UKJ
    I currently use MySql Match function for my search. How do I highlight/bold the words that are matches to the query. Like what Google does...(i.e. if I search for Reebok running shoes, how do I highlight the word Reebok in the product name and running in the description?) Thanks.
    Can you concatenate html tags to the result in your SELECT?

  8. #8
    Member
    Join Date
    April 11th, 2006
    Location
    Michigan
    Posts
    70
    Yes, there is a way to do it. Please show me the example, I might be able to guide you.
    Burhan Tanweer
    Marketing Manager
    sales/at/awebhosts.com
    [URL=http://www.awebhosts.com]www.awebhosts.com [/URL]

  9. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Selling Live streaming matches
    By arponchakma in forum Introduce Yourself
    Replies: 4
    Last Post: December 12th, 2014, 03:47 AM
  2. The search is complete. No matches were found.
    By walleyewarrior in forum Midnight Cafe'
    Replies: 8
    Last Post: June 4th, 2004, 09:05 AM
  3. The search is complete. No matches were found.
    By walleyewarrior in forum Midnight Cafe'
    Replies: 2
    Last Post: April 19th, 2004, 06:11 AM
  4. exact phrase matches (google)
    By Heyder in forum Search Engine Optimization
    Replies: 19
    Last Post: February 17th, 2003, 08:34 AM
  5. Altavista - Sponsored Matches
    By Freebie Hawk in forum Search Engine Optimization
    Replies: 1
    Last Post: September 3rd, 2002, 09:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •