我想弄清楚在這段代碼中出了什麼問題。它要麼不突出顯示搜索結果,要麼輸出圍繞突出顯示的文本的html標籤。 。突出顯示在php錯誤中的搜索結果
$search_result = "";
$search_result = trim($search_result);
$special_cases = array('%', '_', '+');
$search_result = str_replace($special_cases, '', $_GET["q"]);
//Check if the string is empty
if ($search_result == "") {
echo "<p>Search Error</p><p>Please enter a search...</p>" ;
exit();
}
$result = mysql_query('SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes WHERE cQuotes LIKE "%' . mysql_real_escape_string($search_result) .'%" ORDER BY idQuotes DESC', $conn)
or die ('Error: '.mysql_error());
//eliminating special characters
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES);
}
function highlightWords($string, $word)
{
$string = str_replace($word, "<span style='background-color: #FFE066;font-weight:bold;'>".$word."</span>", $string);
/*** return the highlighted string ***/
return $string;
}
?>
<div class="caption">Search Results</div>
<div class="center_div">
<table>
<?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
$cQuote = highlightWords($row['cQuotes'], $search_result);
?>
<tr>
<td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
<td style="font-size:16px;"><?php h($cQuote); ?></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>
上的瀏覽器
,它被輸出作爲:
A good <span style='background-color: #FFE066;font-weight:bold;'>action</span> is an ever-remaining store and a pure yield
,或者如果一個div使用具有類:
A good <div class='highlight'>action</div> is an ever-remaining store and a pure yield
問題是什麼? – 2010-04-30 20:57:13
html標籤顯示爲輸出,而不是格式化並突出顯示關鍵字。 – input 2010-04-30 21:03:44