Highlighting Words in Search Results using PHP
Submitted by alpha_luna on Friday, August 19, 2016 - 15:35.
If you are looking for Highlighting Words in Search Results using PHP then you are at the right place. This simple tutorial will help the users to identify results easily in highlighting those multiple words after searching. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.
Creating one TextBox for the user where they type their desired word to search and one button as shown in the image below.
Here's the source code of the image above.
This simple PHP source code is used to search words in title or description. If match the words, it will be highlighted those keywords. The code is,
Hope that this tutorial will help you a lot.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

- <?php
- $hint_word_search = "";
- $condition_of_Query = "";
- $hint_word_search = $_POST["hint_word_search"];
- $condition_of_Query = " WHERE ";
- for($i=0;$i<$wordsCount;$i++) {
- $condition_of_Query .= "title LIKE '%" . $words_Array[$i] . "%' OR description LIKE '%" . $words_Array[$i] . "%'";
- if($i!=$wordsCount-1) {
- $condition_of_Query .= " OR ";
- }
- }
- }
- $orderby = " ORDER BY id desc";
- $sql = "SELECT * FROM links " . $condition_of_Query;
- ?>
- <?php
- function words_highlighted($text, $hint_word_search) {
- for($i=0;$i<$wordsCount;$i++) {
- $highlighted_text = "<span style='font-weight:bold;'>$words_Array[$i]</span>";
- }
- return $text;
- }
- ?>
Result

Add new comment
- 239 views