簡體   English   中英

突出顯示PHP錯誤中的搜索結果

[英]highlighting search results in php error

我試圖找出這段代碼有什么問題。 它要么不突出顯示搜索結果,要么輸出圍繞突出顯示的文本的html標簽。

$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

您的輸出函數h()轉義了所有html字符( htmlspecialchars

更改:

$cQuote =  highlightWords($row['cQuotes'], $search_result);

至:

$cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);

並更改:

<td style="font-size:16px;"><?php h($cQuote); ?></td>

至:

<td style="font-size:16px;"><?php echo $cQuote ?></td>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM