显示多个单词的搜索结果

发布于 2024-08-31 00:17:13 字数 2500 浏览 7 评论 0原文

在我的搜索表单中,如果用户输入“good”,它会显示包含关键字“good”的所有结果。然而,如果用户输入“good sweetest”,则不会显示任何结果,因为没有这两个词同时出现的记录;但出现在不同地方的条目中。

例如,记录说:

好的行动是永存的 存储和纯产量

“good”时,它会显示该记录,但如果用户输入“good”+“pure”,则不会显示任何内容。或者,如果记录包含关键字“good-deeds”,并且用户输入不带连字符的“good deeds”,则不会显示任何内容。

我想要的是,如果用户输入“好”+“纯粹”或“善行”,它应该记录包含这些关键字的突出显示它们。

search.php 代码:

$search_result = "";

$search_result = $_POST["q"];

$search_result = trim($search_result);

//Check if the string is empty
if ($search_result == "") {
  echo  "<p class='error'>Search Error. Please Enter Your Search Query.</p>" ;
  exit();
      }

if ($search_result == "%" || $search_result == "_" || $search_result == "+" ) {
  echo  "<p class='error1'>Search Error. Please Enter a Valid Search Query.</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());


function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
} 

function highlightWords($string, $word)
 {

    $string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</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(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:18px;"><?php h($row['cArabic']); ?></td>
            <td style="font-size:16px;"><?php echo $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>

search.html:

   <form name="myform" class="wrapper">
      <input type="text" name="q" onkeyup="showUser()" class="txt_search"/>
      <input type="button" name="button" onclick="showUser()" class="button"/>
      <p>
        <div id="txtHint"></div>
    </form>

in my search form, if the user types 'good', it displays all the results which contain the keyword 'good'. however if the user types in 'good sweetest', it displays no results because there is no record with the two words appearing together; BUT appearing in an entry at different places.

for example, the record says:

A good action is an ever-remaining
store and a pure yield

the user types in 'good', it will show up this record, but if the user types in 'good' + 'pure', it will not show anything. or if the record contains the keyword 'good-deeds' and if the user types in 'good deeds' without the hyphen, it will not show anything.

what i would like is that if the user types in 'good' + 'pure' or 'good deeds' it should records containing these keywords highlighting them.

search.php code:

$search_result = "";

$search_result = $_POST["q"];

$search_result = trim($search_result);

//Check if the string is empty
if ($search_result == "") {
  echo  "<p class='error'>Search Error. Please Enter Your Search Query.</p>" ;
  exit();
      }

if ($search_result == "%" || $search_result == "_" || $search_result == "+" ) {
  echo  "<p class='error1'>Search Error. Please Enter a Valid Search Query.</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());


function h($s) {
    echo htmlspecialchars($s, ENT_QUOTES);
} 

function highlightWords($string, $word)
 {

    $string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</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(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:18px;"><?php h($row['cArabic']); ?></td>
            <td style="font-size:16px;"><?php echo $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>

search.html:

   <form name="myform" class="wrapper">
      <input type="text" name="q" onkeyup="showUser()" class="txt_search"/>
      <input type="button" name="button" onclick="showUser()" class="button"/>
      <p>
        <div id="txtHint"></div>
    </form>

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

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

发布评论

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

评论(3

与酒说心事 2024-09-07 00:17:13

您想要的是全文搜索。简而言之,全文搜索基本上是单独搜索每个单词。

What you want is called full-text searching. Put simply, full-text searching basically searches for each individual word on its own.

゛清羽墨安 2024-09-07 00:17:13

您将需要实现内容索引来利用这种搜索,本质上是一个将每个单词与内容组件(如您的记录)相关联的关系表,当您搜索查询索引并返回关联的内容时,这也允许您更改按计数相关性、添加要索引的字段等。http://www.databasejournal.com/sqletc/article.php/1578331/Using-Fulltext-Indexes-in-MySQL---Part-1.htm 这可能是一个好的开始。

You will want to implement content indexing to utilize this kind of search, essentially a relational table associating each word with a content component (like your record there), when you search query the index and return the associated content, also this allows you to change relevance by count, add fields to be indexed etc. http://www.databasejournal.com/sqletc/article.php/1578331/Using-Fulltext-Indexes-in-MySQL---Part-1.htm this might be a good start.

迷你仙 2024-09-07 00:17:13

在我看来,为您的网站正确创建搜索引擎是最困难的事情之一。我只需在谷歌上注册我的网站并让谷歌对其进行索引即可。您可以自定义页面的输出并将其外观集成到您的网站中。

唯一的证据是“Google”品牌的搜索按钮,但说实话,我认为你现在可以删除它。

In my opinion, doing a search engine correctly for your site is one of the toughest things to do. I would just sign my site up on google and let google index it. You can customize the output of the page and integrate that look into your site.

The only evidence will be the "Google" branded search button, but to be honest, I think you can remove that now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文