toanld / similar-text-finder
模糊搜索,相似文本查找器:"你是想说`foo`吗?"
1.1.0
2019-02-18 08:22 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-09-19 13:44:41 UTC
README
TL;DR(太长不看)
相似文本查找器。通过composer安装,适用于任何框架:Laravel, Slim, Symfony等。
// Init Similar Text Finder with a needle and a haystack $text_finder = new \SimilarText\Finder('bananna', ['apple', 'banana', 'kiwi']); // Get first similar word (it's banana) echo $text_finder->first();
安装
使用composer安装 composer require rap2hpoutre/similar-text-finder
。就这样。
你现在可以在你的框架控制器中使用它,或者你想要的任何地方(你实际上不需要框架)。
使用方法
快速入门
只需创建一个新的相似文本查找器,如下所示
$tf = new \SimilarText\Finder($needle, $haystack);
$needle
可能是用户输入,而$haystack
应该是包含所有建议的数组。你可以这样显示最接近的响应
echo 'Did you mean ' $tf->first() . ' ?';
或者在您喜欢的模板引擎(Twig, Blade等)中使用它
这样你可以按最接近的词语顺序获取所有建议
$all = $tf->all();
原始PHP示例
use SimilarText\Finder; // User input with a typo (you could get it from $_GET) $needle = 'tmatoes'; // Your list (from your database or an API) $haystack = ['salad', 'tomatoes', 'onions', 'mates']; // Init Text Finder $finder = new Finder($needle, $haystack); // Display all results ordered by the most approching $results = $finder->all(); echo implode(', ', $results); // You should see something like "tomatoes, mates, onions, salad", yohoo.