Highlighting Words in Search Results using PHP

Submitted by: 
Language: 
Visitors have accessed this post 640 times.

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.

Result

Here's the source code of the image above.

  1. <form method="post" action="">
  2. <div class="search-box">
  3. <label class="label_word">Enter Search Word:</label>
  4. <div>
  5. <input type="text" name="hint_word_search" class="example_Box" autofocus="autofocus" value="<?php echo $hint_word_search; ?>" />
  6. </div>
  7. <div>
  8. <input type="submit" name="go" class="search_Button" value="Search">
  9. </div>
  10. </div>
  11. </form>

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,

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "links");
  3. $hint_word_search = "";
  4. $condition_of_Query = "";
  5. if(!empty($_POST["hint_word_search"])) {
  6. $hint_word_search = $_POST["hint_word_search"];
  7. $words_Array = explode(" ", $hint_word_search);
  8. $wordsCount = count($words_Array);
  9. $condition_of_Query = " WHERE ";
  10. for($i=0;$i<$wordsCount;$i++) {
  11. $condition_of_Query .= "title LIKE '%" . $words_Array[$i] . "%' OR description LIKE '%" . $words_Array[$i] . "%'";
  12. if($i!=$wordsCount-1) {
  13. $condition_of_Query .= " OR ";
  14. }
  15. }
  16. }
  17. $orderby = " ORDER BY id desc";
  18. $sql = "SELECT * FROM links " . $condition_of_Query;
  19. $result = mysqli_query($conn,$sql);
  20. ?>
  21. <?php
  22. function words_highlighted($text, $hint_word_search) {
  23. $words_Array = explode(" ", $hint_word_search);
  24. $wordsCount = count($words_Array);
  25.  
  26. for($i=0;$i<$wordsCount;$i++) {
  27. $highlighted_text = "<span style='font-weight:bold;'>$words_Array[$i]</span>";
  28. $text = str_ireplace($words_Array[$i], $highlighted_text, $text);
  29. }
  30. return $text;
  31. }
  32. ?>

Result

Result

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.