PHP code example of designbycode / fuzzy-search

1. Go to this page and download the library: Download designbycode/fuzzy-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

designbycode / fuzzy-search example snippets


use Designbycode\FuzzySearch\FuzzySearch;

$texts = ['apple', 'banana', 'orange', 'grape'];
$fuzzySearch = new FuzzySearch($texts, true); // Case-insensitive search

$query = 'aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: ['apple']

$bestMatch = $fuzzySearch->getBestMatch($results);
echo $bestMatch; // Output: 'apple'

use Designbycode\FuzzySearch\LevenshteinDistance;

$str1 = 'kitten';
$str2 = 'sitting';
$distance = LevenshteinDistance::calculate($str1, $str2);
echo $distance; // Output: 3

$texts = ['Apple', 'Banana', 'Orange', 'Grape'];
$fuzzySearch = new FuzzySearch($texts, true);

$query = 'aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: ['Apple']

$texts = ['apple', 'banana', 'orange', 'grape'];
$fuzzySearch = new FuzzySearch($texts, false);

$query = 'Aple';
$maxDistance = 2;
$results = $fuzzySearch->search($query, $maxDistance);
print_r($results); // Output: []