Results 1 to 8 of 8

Thread: Highlighting searched keywords

  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Highlighting searched keywords

    Hello,
    I'm wondering if this is possible: when my users search in a free text box and results are returned in php/mysql...is it possible to somehow highlight the words they searched for in the returned records?

    thanks,
    sage

  2. #2
    Join Date
    Mar 2006
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Default

    Well I can say that I have never done this but, the first thing that came to my mind was this...

    PHP Code:
    <?php
    //Get the search string... search.php?q=content
    $q $_REQUEST['q'];

    $content "This is all of your content from your mysql database. Get it here however";


    //Highlight the keywords
    $updatedContent str_replace($q"<span style=\"background-color:yellow\">" $q "</span>"$content);

    echo 
    $updatedContent;

    ?>
    Maybe someone else knows of a better way of doing this... But ive never done search scripts, but the concept above would work...

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Instead of using this line:

    Code:
    $updatedContent = str_replace($q, "<span style=\"background-color:yellow\">" . $q . "</span>", $content);
    try using this instead so that the search is case-insensitive:

    Code:
    $updatedContent = str_ireplace($q, "<span style=\"background-color:yellow\">" . $q . "</span>", $content);
    Hope this helps
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Mar 2006
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Default

    Good thinking testing... That didnt even dawn on me...

  5. #5
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default str_ireplace issue

    Hello:

    I just used str_ireplace for a search script but came across an issue. Here's my code:

    PHP Code:
    str_ireplace($var'<span class="highlight_pink">'.$var.'</span>'$FirstName); 
    The problem I'm seeing is that when I search for the word 'solar' and there are records in the database that match the result set changes the case of the word. So 'SOLAR' or 'Solar' is displayed on screen as 'solar.' I'd like to keep the case of the database record and not change it to whatever was put into the search field.

    Any ideas on what to do?

    Thanks!

  6. #6
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    try
    PHP Code:
    preg_replace('/('.$var.')/i''<span class="highlight_pink">$1</span>'$FirstName); 
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  7. #7
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thank you!

    Thank you Master Script Maker!!! That worked beautifully!

  8. #8
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Your welcome! There's a button for thanks too .
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

Bookmarks

Posting Permissions

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