Search
 
SCRIPT & CODE EXAMPLE
 

PHP

levenshtein (PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8) levenshtein — Calculate Levenshtein distance between two strings

<?php
// input misspelled word
$input = 'carrrot';

// array of words to check against
$words  = array('apple','pineapple','banana','orange',
                'radish','carrot','pea','bean','potato');

// 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 :: binding instances laravel 
Php :: phpunit exception message contains string 
Php :: eager loading set limit to relationship 
Php :: php loop through array shorthand 
Php :: laravel view-model 
Php :: Insert Data using modal 
Php :: identify the php function used to get values submitted through a form without using any database? 
Php :: Finding Vulnerable Urls 
Php :: hook de pré-commit se déclenche 
Php :: wordpress show notice only on plugin page 
Php :: validation ignored rules 
Php :: function wp_maintenance_mode() { 763 
Php :: install PHP extension "amqp" not found, please install it 
Php :: php iife 
Php :: registration welcome email laravel 
Php :: wc php get order get coupon discount amount 
Php :: laravel array validation 
Php :: laravel get fetch api request body 
Php :: what does ? mean in php 
Php :: laravel jobs tutorial 
Php :: codes for php 
Php :: how to convert youtube video to mp3 in php 
Php :: wordpress header.php 
Php :: php login system 
Java :: The import javax.persistence cannot be resolved 
Java :: how to clear terminal in java 
Java :: java initialize list with values 
Java :: java int to roman 
Java :: change port in spring boot 
Java :: bukkit event list 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =