2015-11-03 69 views
1

我有一個來自Android系統的字符串。 我收到該字符串,對應於一個PHP文件中的地址,並且想要檢查這個地址是否與我的數據庫中的地址匹配。 但是,我希望它可以工作,無論用戶輸入的單詞的順序如何。php - 從數據庫中查找文本中的多個單詞

起初,我用LIKE如下:

$address=preg_replace('#\s+#','%',$_POST['address']); 
    $address='%'.$address.'%'; 
    echo $address; 
    $list_business = $bdd->prepare('SELECT * FROM business WHERE address LIKE ? OR name_business LIKE ?'); 
    $list_business->execute(array($address,$address)); 

這其實可以讓我即使一些單詞中省略的結果。例如443街將匹配443第一街。 但它不會返回任何東西,如果用戶鍵入id爲第一街443. 我在考慮正則表達式,但它真的適應這種問題?或者我應該爲每個單詞使用一個不同的正則表達式?

謝謝您事先=)

Q

+0

你可以做多'likes'每字輸入的演示。它會給你回來多個記錄。全文搜索可能是更好的方法。 http://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html – chris85

+0

展望未來,謝謝=) – Kunting

回答

0

你可能想看看第三方搜索引擎,會爲你做這個很容易。

按順序查看SOLR,ElasticSearch和Sphinx。

你可以用Mysql的全文搜索來做到這一點,但上面的搜索引擎可以做得更好。

下面是使用MySQL內置的全文檢索

mysql> show tables; 
+----------------+ 
| Tables_in_test | 
+----------------+ 
| sample   | 
+----------------+ 
1 row in set (0.00 sec) 

mysql> show create table test.sample; 
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| Table | Create Table                                      | 
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| sample | CREATE TABLE `sample` (
    `id` int(11) NOT NULL, 
    `address` text, 
    PRIMARY KEY (`id`), 
    FULLTEXT KEY `ftext` (`address`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 | 
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select * from test.sample; 
+----+-----------------------------------------+ 
| id | address         | 
+----+-----------------------------------------+ 
| 1 | 345 Park Avenue New York, NY 10154-0102 | 
| 2 | 610 Waring Ave Bronx, New York NY 10467 | 
+----+-----------------------------------------+ 
2 rows in set (0.00 sec) 

mysql> SELECT * FROM test.sample 
    -> WHERE match(`address`) 
    -> against('+new +york +ave' IN BOOLEAN MODE); 
+----+-----------------------------------------+ 
| id | address         | 
+----+-----------------------------------------+ 
| 1 | 345 Park Avenue New York, NY 10154-0102 | 
| 2 | 610 Waring Ave Bronx, New York NY 10467 | 
+----+-----------------------------------------+ 
2 rows in set (0.00 sec) 

mysql> SELECT * FROM test.sample 
    -> WHERE match(`address`) 
    -> against('+ny +park' IN BOOLEAN MODE); 
+----+-----------------------------------------+ 
| id | address         | 
+----+-----------------------------------------+ 
| 1 | 345 Park Avenue New York, NY 10154-0102 | 
+----+-----------------------------------------+ 
1 row in set (0.00 sec) 

mysql> 
+0

完美的作品,謝謝!現在我將堅持使用MySQL全文,但正如您所指出的那樣,它似乎並不是最好的方法,所以稍後我會優化SOLR。 再次感謝您=) – Kunting

相關問題