• We just launched and are currently in beta. Join us as we build and grow the community.

Highlighting Words in Search Results using PHP

nonfound

SMM Specialist
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
205
Likes
144
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.

⚙ Live Demo

Creating one TextBox for the user where they type their desired word to search and one button as shown in the image below.
1_55.jpg

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. for

    (

    $i

    =

    0

    ;

    $i

    <

    $wordsCount

    ;

    $i

    ++

    )

    {
  26. $highlighted_text

    =

    "<span style='font-weight:bold;'>$words_Array

    [$i

    ]</span>"

    ;
  27. $text

    =

    str_ireplace

    (

    $words_Array

    [

    $i

    ]

    ,

    $highlighted_text

    ,

    $text

    )

    ;
  28. }
  29. return

    $text

    ;
  30. }
  31. ?>

Result

final.jpg


⚙ Live Demo

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top