在php/mysql中突出显示搜索结果 [英] highlighting search results in php/mysql
本文介绍了在php/mysql中突出显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何突出显示使用php的mysql查询的搜索结果?
how do i highlight search results from mysql query using php?
这是我的 [修改] 代码:
$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>
推荐答案
您可以使用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;
- 第一个是您要寻找的东西
- 第二个应该更改为
- 他应该从中搜索并替换的文本字符串
例如
<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.
这篇关于在php/mysql中突出显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文