【问题标题】:php - Find multiple words in text from databasephp - 从数据库中查找文本中的多个单词
【发布时间】:2015-11-03 13:48:02
【问题描述】:

我有一个来自 Android 系统的字符串。 我收到该字符串,对应于 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 Street 将匹配 443 First Street。 但如果用户将 id 输入为 First Street 443,它不会返回任何内容。 我在考虑正则表达式,但它真的适合这类问题吗?或者我应该每个单词使用一个不同的正则表达式?

【问题讨论】:

标签: php mysql regex sql-like


【解决方案1】:

您可能想看看第三方搜索引擎,它们会很容易地为您做到这一点。

按顺序查看 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> 

【讨论】:

  • 完美运行,谢谢!我现在会坚持使用 MySQL 全文,但正如你所指出的,它似乎不是最好的方法,所以我稍后会使用 SOLR 进行优化。再次感谢您=)
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2018-04-03
  • 2016-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多