Stars: 132
Forks: 16
Pull Requests: 8
Issues: 2
Watchers: 11
Last Updated: 2023-09-01 06:42:37
:honeybee: PHP Similar Text Finder aka Fuzzy search. `Did you mean "banana"?`
License: MIT License
Languages: PHP
Similar text finder. Install via composer, works with any framework: Laravel, Slim, Symfony, etc.
// 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();
Install with composer
composer require rap2hpoutre/similar-text-finder
.
That's all.
You can now use it in your framework's controller, or wherever you want (you don't need a framework anyway).
Just build a new Similar Text Finder like this:
$tf = new \SimilarText\Finder($needle, $haystack);
$needle
may be the user input and $haystack
should be an array with all your suggestion. You can display the closest response like this:
echo 'Did you mean ' $tf->first() . ' ?';
Or use it in your favorite template engine (Twig, Blade, etc.)
You can get all your suggestion ordered by most approching words like this:
$all = $tf->all();
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.