Hi all I'm just wondering How can I highlight a search results for example...

I have in my search form a text field so If I write "li" my search return "limp Bizkit, linkin park, lit" etc,...

What I want to know is how can I automatically highlight the word "li" in my results ??

    Hi,

    you could use str_replace to replace li with <b>li</b> in your search result output to display it bold (or to replace li with e.g. <font class="high">li</font>)

      $search_results = str_replace('li', '<span class="search_term">li</span>', $search_results);
      

      In your CSS:

      .search_term {
      background-color: yellow;
      }
      

      Guess that doesn't really do the whole word that contains "li", but its a start.. I think..

        18 days later

        ok i got this code somewhat to work with a search egine im making.

        next question. what if the word to be highlighted for contains more than one word, i dont want it to search for that exact string,rather highlight the words invidually:

        Lets say I have the sentence:

        My dog jumped over the fence and it broke its leg

        here is the code i havenow

        $code = str_replace($hlt, "<span class=search_term>$hlt</span>", $code);

        ok, now lets say $hlt = "dog broke fence"

        Well, of course, the ONLY WAY anythign will be highlighted is if the EXACT phrase "dog broke fence". In the sentene above, it isnot.

        I want each word highlited individually. Any ideas on how this is possible?

          break up the string and search for all of them

          here is a example

          <?
          //Get Search and split it up
          //$query is the search string
          $query2 = explode(" ",$query);
            for($i=0;$i<count($query);$i++){
              if (strstr($str,$query[$i]) && $query[$i]!="")
                 print $query2[$i]." is into the string!";
          
          ##################################
          ## Database Connection For Search
          ##################################
          //Connect to database
          $connect = mysql_connect($db_connetion, $db_username, $db_password) 
          	or die ("<b><center>".mysql_errno().": ".mysql_error()."</b></center>\n");
          $db = mysql_select_db($db_databse, $connect) 
          	or die ("<b><center>".mysql_errno().": ".mysql_error()."</b></center>\n");
          //Now lets do the mysql search, loop for each word
          $sql = "SELECT * from TABLE WHERE FIELD LIKE '%".$query2[$i]."%'";
          $sql_result = mysql_query($sql, $connect) 
          	or die ("<b><center>".mysql_errno().": ".mysql_error()."</b></center>\n");
          $result = mysql_query($sql, $connect) 
          	or die ("<b><center>".mysql_errno().": ".mysql_error()."</b></center>\n");; 
          }
          //Close conecetion
          mysql_close($connect);
          ?>
          

          hope this helps

            Write a Reply...