在php/mysql中突出显示搜索结果

在php/mysql中突出显示搜索结果,php,mysql,search,highlighting,Php,Mysql,Search,Highlighting,如何使用php突出显示mysql查询的搜索结果 这是我的[修改]代码: $search_result = ""; $search_result = $_GET["q"]; $result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . $search_result .'%" ORDER BY idQuotes DESC', $conn) o

如何使用php突出显示mysql查询的搜索结果

这是我的[修改]代码:

$search_result = "";

$search_result = $_GET["q"];

$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . $search_result .'%" ORDER BY idQuotes DESC', $conn)
  or die ('Error: '.mysql_error());


function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
} 


?>

    <div class="caption">Search Results</div>
<div class="center_div">
<table>
    <?php while ($row= mysql_fetch_array($result)) { ?>
    <?php $cQuotes = preg_replace($search_result, "<div class='highlight'>".$search_result."</div>", $row['cQuotes']); ?>
        <tr>
            <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']) ?></td>
            <td style="font-size:16px;"><?php h($cQuotes) ?></td>
            <td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
            <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
        </tr>
    <?php } ?>
</table>
</div>
$search_result=”“;
$search\u result=$\u GET[“q”];
$result=mysql\u query('SELECT cQuotes,vAuthor,cArabic,vReference FROM thquotes,其中cQuotes如“%”.$search\u result.'%”按idQuotes DESC,$conn排序)
或者死('Error:'.mysql_Error());
功能h($s){
echo htmlspecialchars($s,ENT_报价);
} 
?>
搜索结果

您可以通过使用名为的JavaScript库来实现这一点。它工作得很好。

您可以使用preg_replace();,当它在文本中找到匹配项时,您可以在匹配的单词周围放置一个带有一类突出显示的div。然后将背景色和边框添加到highlight类中,使其突出显示

preg_替换3个参数

  • 第一个是你要找的
  • 第二个问题是应该改成什么
  • 他应该从中搜索和替换的文本字符串
  • 比如说

    <div class="center_div">
        <table>
        <caption>Search Results</caption>
        <?php while ($row= mysql_fetch_array($result)) { ?>
    <?php $arabic = preg_replace("/".$search_result."/", "<div class='highlight'>".$search_result."</div>", h($row['cArabic'])); ?>
                <tr>
                    <td style="text-align:right; font-size:15px;"><?php $arabic ?></td>
                    <td style="font-size:16px;"><?php h($row['cQuotes']) ?></td>
                    <td style="font-size:12px;"><?php h($row['vAuthor']) ?></td>
                    <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']) ?></td>
                </tr>
            <?php } ?>
        </table>
        </div>
    
    
    搜索结果
    
    谢谢,但是我想用php来做。谢谢你的帮助。我是这么做的,只是为了引用<代码>
    。但是,这根本没有显示引号记录[它显示的是阿拉伯语、参考文献和作者]。你的意思是它没有将正确的内容放在应该放的地方吗?真奇怪。您能否更改问题中的代码以显示您当前拥有的内容?这将更容易看到不正确的地方,因为它没有显示cQuotes记录中的任何内容。修改了代码。我在上面的回答中更新了代码,您需要在preg_replace函数的第一个参数之前和之后添加/preg_replace。我看到,当我测试代码并收到该错误时,我猜您已经关闭了错误报告或隐藏了错误报告。经过几次修改后,它现在显示了记录,但仍然没有突出显示搜索的关键字<代码>