rap2hpoutre / 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-08-30 01:32:05 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.