TrO*_*nNe 5 php regex full-text-search regex-group levenshtein-distance
我有一个包含以下各列的MySQL表:
City Country Continent
New York States Noth America
New York Germany Europe - considering there's one ;)
Paris France Europe
Run Code Online (Sandbox Code Playgroud)
如果我想找到带有错字的“ New Yokr”,那么使用MySQL存储函数很容易:
$querylev = "select City, Country, Continent FROM table
WHERE LEVENSHTEIN(`City`,'New Yokr') < 3"
Run Code Online (Sandbox Code Playgroud)
但是,如果有两个纽约城市,以全文搜索,则可以输入“ New York States”,您会得到想要的结果。
所以问题是,我可以搜索“ New Yokr Statse”并获得相同的结果吗?
是否有任何将levenshtein和全文本合并以形成一个完整解决方案的函数,还是应该在MySQL中创建一个将三列连接在一起的新列?
我知道还有其他解决方案,例如lucene或Sphinx(也有soundex,metaphone,但对此无效),但我认为对我来说可能很难实现。
很好的问题,也是我们如何使用字符列表和正则表达式边界来设计查询和检索我们想要的数据的一个很好的例子。
根据我们可能想要的准确性以及数据库中的数据,我们当然可以基于各种表达式设计自定义查询,例如以下具有New York State各种类型的示例:
([new]+\s+[york]+\s+[stae]+)
Run Code Online (Sandbox Code Playgroud)
在这里,我们有三个字符列表,我们可以用其他可能的字母更新它们。
[new]
[york]
[stae]
Run Code Online (Sandbox Code Playgroud)
我们还在这里添加了两组作为\s+边界以提高准确性。
此代码片段仅显示捕获组的工作原理:
([new]+\s+[york]+\s+[stae]+)
Run Code Online (Sandbox Code Playgroud)
$re = '/([new]+\s+[york]+\s+[stae]+)/mi';
$str = 'Anything we wish to have before followed by a New York Statse then anything we wish to have after. Anything we wish to have before followed by a New Yokr State then anything we wish to have after. Anything we wish to have before followed by a New Yokr Stats then anything we wish to have after. Anything we wish to have before followed by a New York Statse then anything we wish to have after. ';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)