且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-03 17:50:28

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

you could use preg_replace();, when it finds a match in your text you could place a div with a class of highlight around the matching word. You would then add a background color and border to the highlight class to make it stand out

preg_replace期望3个参数;

preg_replace expect 3 parameters;

  1. 第一个是您要寻找的东西
  2. 第二个应该更改为
  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>

我只为阿拉伯语做过,但是您可能还需要为cQuotes,vAuthor和vReference做这件事.

I only did it for arabic but you might need to do that for cQuotes, vAuthor and vReference as well.