Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    Highlighting search terms in results

    Hello,

    I have a search form that returns results, and I'd like to highlight the search term in the results e.g. if the user searched for North, was the word North found in the House Name, or Town, or country etc.

    Is there an easier way to do this than checking to see if every word in the returned row matches the search term?

    I'm using php and mySQL.

    Thanks for your help!

    Nicky

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    function highlight($word$color$text)
    {
        
    $word preg_quote($word"#");
        
    $text preg_replace("#".$word."#is""<span style=\"color:".$color."\">".$word."</span>"$text);
        return 
    $text;
    }

    $text "foo bar barfoo foobar";
    $text highlight("foo""#00CC33"$text);

    echo 
    $text;

    ?>
    your welcome
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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