Lazy Levenshtein

PHP has a built-in Levenshtein distance function. I wanted to have one that used lazy evaluation to avoid checking matrix values that wouldn't be used. That is what this is - a lazy evaluation of Levenshtein distance.

Download

Click Here for the Source Code. This is saved as a text file so the web server won't mess with the code. You will want to save it with a "php" extention, not "txt".

Usage

$diff = lazy_levenshtein($string1, $string2)
$diff = lazy_levenshtein($string1, $string2, $cost_ins, $cost_rep, $cost_del, $prefill_inc)
The first example is the simplest version - just calculates Levenshtein distance. The second example shows all the options for altering the default costs and prefill values. I purposely designed this so you can replace all calls to "levenshtein" with "lazy_levenshtein".

Purpose

Please note: This function is not faster than using the built-in levenshtein function! This function was designed to be used for statistical analysis of lazy evaluation of Levenshtein distance, compared to calculation of the entire matrix. If you are looking for speed, use the built-in function. It is as optimized as you're gonna get with PHP.