<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>How to Highlight Matched Keyword in Search using PHP</title> <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css"> <style type="text/css"> .mt20{ margin-top:20px; } </style> </head> <body> <div class="container"> <h1 class="text-center mt20">Highlight Matched Keyword in Search</h1> <div class="row justify-content-center mt20"> <div class="col-sm-6"> <form method="POST"> <div class="input-group input-group-lg"> <input type="text" name="keyword" class="form-control" placeholder="Firstname, Lastname or Nickname" aria-describedby="basic-addon2"> <div class="input-group-append"> <button class="btn btn-outline-secondary" type="submit" name="search">Search</button> </div> </div> </form> <?php $keyword = $_POST['keyword']; //connection $conn = new mysqli('localhost', 'root', '', 'mydb'); $sql = "SELECT * FROM members WHERE firstname LIKE '%$keyword%' OR lastname LIKE '%$keyword%' OR nickname LIKE '%$keyword%'"; $query = $conn->query($sql); if($query->num_rows > 0){ echo "<h5 class='mt20'>Search results for '<i>".$keyword."</i>'</h5> <ul> "; while($row = $query->fetch_assoc()){ $fullname = $row['firstname'].' "'.$row['nickname'].'" '.$row['lastname']; echo "<li>".$highlighted."</li>"; } echo "</ul>"; } else{ echo " <h5 class='mt20'>No search results found for '<i>".$keyword."</i>'</h5> "; } } ?> </div> </div> </div> </body> </html>