Jump to content

Highlight After Search


kkkaiden
 Share

Recommended Posts

Im using this code to search my database:

<?phpinclude'connect.php';mysql_select_db($dbname);$search=$_POST["search"];echo "<div class='searchheader'><h2 style='margin: auto;'>Search results for '$search'</h2></div>";$result = mysql_query("SELECT * FROM table WHERE title OR description LIKE '%$search%'");while($r=mysql_fetch_array($result)){ $title=$r["title"]; $description=$r["description"]; echo "<div class='text alternative'><h2>$title</h2>" . "<p>$description</p></div>";}?>
After it finds the results I want the $search to be highlighted, how would I do that?
Link to comment
Share on other sites

You can perform a str_replace() on the $title and $description to add a highlight, e.g.

"<p>" . str_replace($search, "<span class=\"highlight\">$search</span>", $description) . "</p></div>";

Link to comment
Share on other sites

You can perform a str_replace() on the $title and $description to add a highlight, e.g.
"<p>" . str_replace($search, "<span class=\"highlight\">$search</span>", $description) . "</p></div>";

thanks, can you tell me how to make my search only find whole words and make it so it isnt case sensitive.
Link to comment
Share on other sites

To be case insensitive, just convert everything to lowercase before comparing.To search for only whole words, you can search for the phrase surrounded by spaces.Note you also have a logical error in your code, you have to compare each field individually.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...