nonfound
SMM Specialist
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.
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,
Result
⚙ 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
⚙ 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.

Here's the source code of the image above.
- <form
method
=
"post"
action
=
""
>
- <div
class
=
"search-box"
>
- <label
class
=
"label_word"
>
Enter Search Word:</
label
>
- <div
>
- <input
type
=
"text"
name
=
"hint_word_search"
class
=
"example_Box"
autofocus=
"autofocus"
value
=
"<?php echo $hint_word_search; ?>
" />
- </
div
>
- <div
>
- <input
type
=
"submit"
name
=
"go"
class
=
"search_Button"
value
=
"Search"
>
- </
div
>
- </
div
>
- </
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,
- <?php
- $conn
=
mysqli_connect
(
"localhost"
,
"root"
,
""
,
"links"
)
;
- $hint_word_search
=
""
;
- $condition_of_Query
=
""
;
- if
(
!
empty
(
$_POST
[
"hint_word_search"
]
)
)
{
- $hint_word_search
=
$_POST
[
"hint_word_search"
]
;
- $words_Array
=
explode
(
" "
,
$hint_word_search
)
;
- $wordsCount
=
count
(
$words_Array
)
;
- $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
;
- $result
=
mysqli_query
(
$conn
,
$sql
)
;
- ?>
- <?php
- function
words_highlighted(
$text
,
$hint_word_search
)
{
- $words_Array
=
explode
(
" "
,
$hint_word_search
)
;
- $wordsCount
=
count
(
$words_Array
)
;
- for
(
$i
=
0
;
$i
<
$wordsCount
;
$i
++
)
{
- $highlighted_text
=
"<span style='font-weight:bold;'>$words_Array
[$i
]</span>"
;
- $text
=
str_ireplace
(
$words_Array
[
$i
]
,
$highlighted_text
,
$text
)
;
- }
- return
$text
;
- }
- ?>
Result

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