highlighting search results in php/mysql

前端 未结 2 673
南方客
南方客 2020-12-07 03:44

how do i highlight search results from mysql query using php?

this is my [modified] code:

$search_result = \"\";

$search_result = $         


        
相关标签:
2条回答
  • 2020-12-07 04:22

    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 expect 3 parameters;

    1. the first one is what you are looking for
    2. second one is what should it be changed to
    3. The string of text he should search and replace from

    so for example

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

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

    0 讨论(0)
  • 2020-12-07 04:33

    You can do that by using a JavaScript library called HiLite. It works pretty well.

    0 讨论(0)
提交回复
热议问题