highlight for keyword on the result search

Discussion in 'PHP' started by junandya, Jan 20, 2008.

  1. #1
    Hello,

    i have a search engine. And i want to make a highlight for each word in the result list which match with the keyword that specified in the form. could someone here give me a script which can do what i need

    Kind regards
     
    junandya, Jan 20, 2008 IP
  2. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Dunno about the script part - but you can define the highlight section in a css file, and then in PHP simply define the highlight part when the phrase match occurs.

    cheers
    GS
     
    pubdomainshost.com, Jan 20, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    // Keywords that the user used
    $keywords = array('i', 'want', 'to', 'find', 'it');
    
    $keywords = array_filter(array_unique($keywords));
    $keywords = array_map('preg_quote', $keywords);
    
    // $text = the search result text
    $text = preg_replace('~(' . $implode('|', $keywords) . ')~i', '<span class="keyword">$1</span>', $text);
    
    PHP:
    Now define a CSS class called "keyword" with the properties you want and you're done.
     
    nico_swd, Jan 20, 2008 IP