PHP 突出显示输入搜索关键字

PHP Highlight Input Search Keyword

我想突出显示每个 $searchkey 结果。 我尝试了其他解决方案,但它们不适用于搜索引擎。 结果出现在 table 中。 我希望关键字的每一次出现都被突出显示。

感谢您的帮助!

<?php
$dbhost = 'xxx';
$username = 'xxx';
$password = 'xxx';

mysql_connect("$dbhost" , "$username", "$password" );
mysql_select_db("xxx") or die("could not find the database!");
$output = '';


if(isset($_GET['search']))
{
    $searchkey = $_GET['search'];
    $query = mysql_query("SELECT * FROM xxx WHERE email LIKE '%$searchkey%' ") or die("Could not search");
    $count = mysql_num_rows($query);

    if ($count == 0) 
    {
        $output = 'There was no search results !' ;
    }
    else 
    {
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>email</th><th>hashkey</th></tr>"; 

        while ($row = mysql_fetch_array($query))
         {

            $email = $row['email'];
            $hashkey = $row['hashkey'];
            echo "<tr><td>"; 
            echo $email;
            echo "</td><td>";   
            echo $hashkey;   
            echo "</td></tr>";  
        }
            echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>';

    }
    echo "</table>";   
    $searchkey= $words;
}
?>

您可以在 PHP 中使用 正则表达式 来执行此操作。在这段代码中我使用了<strong>标签,你可以随意替换它。

while ($row = mysql_fetch_array($query))
{

    $email = preg_replace('/(' . $searchkey . ')/s', '<strong></strong>', $row["email"]);
    $hashkey = $row['hashkey'];
    echo "<tr><td>";
    echo $email;
    echo "</td><td>";  
    echo $hashkey;  
    echo "</td></tr>"; 
}