Re: highlighting Search Results

From: Date: Mon, 06 May 2002 12:11:31 +0000
Subject: Re: highlighting Search Results
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-96240@lists.php.net to get a copy of this message
If you want to highlight the EXACT search string, do a str_replace() on $row["Description"], $row["ProductName"], etc etc, replacing the search phrase with <B>$searchSite</B>. Pretty simple. <? $description = str_replace($siteSearch, "<B>$siteSearch</B>", $row["Description"]); $productName = str_replace($siteSearch, "<B>$siteSearch</B>", $row["ProductName"]); ?> But, I'm guessing the search word is possibly more than one word??? If that's the case, you may wish to highlight each word individually: If that's the case, split the words in $searchSite into an array, and loop through through the array, replacing each word with <B>word</B>. Something like (untested): <? $searchWords = explode(" ", $searchSite); foreach($searchWords as $key => $word) { $row["Description"] = str_replace($word, "<B>$word</B>", $row["Description"]); $row["ProductTitle"] = str_replace($word, "<B>$word</B>", $row["ProductTitle"]); } echo $row["ProductTitle"]."<BR>"; echo $row["Description"]."<BR>"; ?> Although this will need further tweaking, because it's case sensitive (searching for "dog" and "Dog" will produce different results, it doesn't account for dog* (doggies, dogs, etc), and is perhaps a little limited in other ways, but should get you started). A well constructed reg exp instead of the str_replace will cover most of this, but i'm no good at them, and str_replace is quicker, IF it's all you need. Justin French -------------------- Creative Director http://Indent.com.au -------------------- on 06/05/02 9:26 PM, DC (dcurran@powerup.com.au) wrote: > Hi all, > > I have a mySQL database running with PHP4. I have constructed a search form > ($searchSite) which returns results in a results page & now I want to have > the word that was entered in the search form highlight in the results page. > > eg User enters a search for products using the word " gardening" > > Results page might return: > > "this shovel is a great gardening tool" and "to be a expert in gardening, > you will need"...............I want the word gardening to be bold wherever > it is displayed. > > My results page has the following code to display search results: BTW it > works like a dream...I just want to jazz it up a bit. > > <? > > $result = mysql_query("SELECT * FROM products WHERE Description LIKE > '%$searchSite%' OR Keywords LIKE '%$searchSite%' OR CatName LIKE > '%$searchSite%' "); > > blah blah blah > > while ( $row = mysql_fetch_array($result) ) { > echo("<tr bgcolor='#FFFFFF'>"); > echo("<td width='93'><div align='center'><font size= > '2' face = > 'Arial,Helvetica,Sans serif'>" . $row["Description"] . > "</td>"); > echo("<td width='93'><div align='center'><font size= > '2' face = > 'Arial,Helvetica,Sans serif'>" . $row["ProductName"] . > "</td>"); > echo("<td width='93'><div align='center'><font size= > '2' face = > 'Arial,Helvetica,Sans serif'>\$" . $row["RetailPrice"] . > "</td>"); > > ?> > > How do I do this to highlight my search words? > > Thanks, any ideas much appreciated by this PHP newbie. > > DC > >

« previous php.general (#96240) next »