在 php 错误中突出显示搜索结果

发布于 2024-08-30 18:44:45 字数 2085 浏览 8 评论 0原文

我试图找出这段代码有什么问题。它要么不突出显示搜索结果,要么输出突出显示文本周围的 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 与 class 一起使用:

A good <div class='highlight'>action</div> is an ever-remaining store and a pure yield

i'm trying to figure out what is wrong in this code. it either doesn't highlight the search result OR it outputs html tags surrounding the highlighted text. .

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

on the browser, it is outputted as:

A good <span style='background-color: #FFE066;font-weight:bold;'>action</span> is an ever-remaining store and a pure yield

or if a div is used with class:

A good <div class='highlight'>action</div> is an ever-remaining store and a pure yield

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请别遗忘我 2024-09-06 18:44:45

您的输出函数 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>

Your output function h() is escaping all the html characters (htmlspecialchars)

Change:

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

To:

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

And change:

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

To:

<td style="font-size:16px;"><?php echo $cQuote ?></td>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文