2009-04-28 42 views
0

我正试图在我的网站上实现搜索功能。我有以下类型的数据。如何从MYSQL/PHP中的数据中提取匹配短语?

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。

谷歌有时做同样的事情,谷歌显示短语形式中间,有时它显示的起始文本。

谢谢。

回答

-3

如果您有存储在MySQL表的标题和描述,尝试这样的事情:

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

ORDER BY RAND()条件将随机从各行的一个选择,其中标题的条件是World news比赛

+0

我想你没有正确地得到我的问题。我不想随机行。我想要从特定行中随机选择的短语。 – Prashant 2009-04-29 06:29:44

1

不知道是否有MySQL方法来实现你的目标。我会从数据库提取字段,然后使用PHP的爆炸()函数随机短语:

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

谷歌不只是返回一些随机的句子。它会返回包含您搜索的词的片段(或两个)。 MySQL不能为你做到这一点。您需要在PHP中手动执行此操作(手动搜索返回的数据库行中的关键字并显示相关代码段)或使用可为您执行此操作的搜索引擎(如Xapian,Sphinx或Apache Lucene)。