Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Getting the closest string match using php

<?php

$input = 'Director, My Company';

// array of words to check against
$words  = array('Foo bar','Lorem Ispum','Director');

// no shortest distance found, yet
$shortest = -1;

// loop through words to find the closest
foreach ($words as $word) {

    // calculate the distance between the input word,
    // and the current word
    $lev = levenshtein($input, $word);

    // check for an exact match
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $word;
        $shortest = 0;

        // break out of the loop; we've found an exact match
        break;
    }

    // if this distance is less than the next found shortest
    // distance, OR if a next shortest word has not yet been found
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}

echo "Input word: $input
";
if ($shortest == 0) {
    echo "Exact match found: $closest
";
} else {
    echo "Did you mean: $closest?
";
}
Comment

PREVIOUS NEXT
Code Example
Php :: replace twig 
Php :: signup form in php 
Php :: How to Add Custom Fonts to a WordPress Theme 
Php :: utc time php 
Php :: php array serialize 
Php :: laravel access request in provider 
Php :: casts laravel 
Php :: count column eloquent laravel 
Php :: php authentication 
Php :: model class not found in laravel 
Php :: php key_exists 
Php :: php execute a background process 
Php :: active page in laravel 
Php :: last insert id mysqli 
Php :: array_chunk in php 
Php :: Movie Name -inurl:(htm|html|php|pls|txt) intitle:index.of “last modified” (mp4|wma|aac|avi) 
Php :: signup api in laravel 
Php :: comment split une chaine de caratere en php 
Php :: laravel where and where 
Php :: get_the_terms 
Php :: epoch to date php 
Php :: php get last day of month 
Php :: carbon this month first day 
Php :: how to convert enum to string in php 
Php :: auto refresh extintion php 
Php :: php get all days between two dates 
Php :: php artisan serve stop 
Php :: laravel migration change column order 
Php :: php rce command 
Php :: year dropdown loop in php 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =