【问题标题】:How to extract a matching phrase from data in MYSQL/PHP?如何从 MYSQL/PHP 中的数据中提取匹配的短语?
【发布时间】:2009-04-28 11:13:00
【问题描述】:

我正在尝试在我的网站中实现搜索功能。我有以下类型的数据。

Title                Descr  
-----                ----- 
World News           World news, news from across the world, all world news events covered
World Sports         World sports news, for all of sports people, sports is a famous world

现在我想如果用户搜索“世界新闻”然后结果将显示如下,

World News (title)
... all world news events covered ... (descr)

基本上我想随机显示一些短语。就像当用户搜索“世界新闻”时,有时他会得到类似上面的结果,有时会得到下面的结果,

World News (title)
World news, news from across the world, ... (descr)

请告诉我我们如何使用 mysql 查询获得这种功能,或者如果不能直接通过 mysql 查询实现,那么如何使用 PHP。

Google 有时会做同样的事情,Google 从中间显示短语,有时会显示起始文本。

谢谢。

【问题讨论】:

    标签: php mysql search


    【解决方案1】:

    不确定是否有 MySQL 方法可以实现您的目标。我会从数据库中提取字段,然后使用 PHP 的 explode() 函数来随机化一个短语:

    $desc_field_result = "World news, news from across the world, all world news events covered"; // The "Desc" field from your MySQL Query
    
    $desc_field_array = explode(",",$desc_field_result);
    
    $selected_desc = $desc_field_array[rand(0,sizeof($desc_field_array)-1)];
    
    echo $selected_desc;
    

    【讨论】:

      【解决方案2】:

      Google 不只是返回一些随机句子。它返回一个包含您搜索的单词的 sn-p(或两个)。 MySQL 无法为您做到这一点。您需要在 PHP 中手动执行此操作(在返回的数据库行中手动搜索关键字并显示相关的 sn-p)或使用可以为您执行此操作的搜索引擎(例如 Xapian、Sphinx 或 Apache Lucene) .

      【讨论】:

        【解决方案3】:

        如果您的 mysql 表中存储了标题和描述,请尝试以下操作:

        SELECT descr FROM yourTable WHERE title='World News' ORDER BY RAND()
        

        ORDER BY RAND() 条件将随机选择标题条件为World news 匹配的行之一

        【讨论】:

        • 我认为您没有正确回答我的问题。我不想要随机行。我想要从特定行中随机选择的短语。
        猜你喜欢
        • 1970-01-01
        • 2023-03-11
        • 2020-02-12
        • 1970-01-01
        • 2019-01-08
        • 2015-11-23
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        相关资源
        最近更新 更多