51 public static function levenshtein( $str1, $str2, $length1, $length2 ) {
52 if ( $length1 === 0 ) {
55 if ( $length2 === 0 ) {
58 if ( $str1 === $str2 ) {
62 $bytelength1 = strlen( $str1 );
63 $bytelength2 = strlen( $str2 );
64 if ( $bytelength1 === $length1 && $bytelength1 <= 255
65 && $bytelength2 === $length2 && $bytelength2 <= 255
70 $prevRow = range( 0, $length2 );
71 for ( $i = 0; $i < $length1; $i++ ) {
73 $currentRow[0] = $i + 1;
74 $c1 = mb_substr( $str1, $i, 1 );
75 for ( $j = 0; $j < $length2; $j++ ) {
76 $c2 = mb_substr( $str2, $j, 1 );
77 $insertions = $prevRow[$j + 1] + 1;
78 $deletions = $currentRow[$j] + 1;
79 $substitutions = $prevRow[$j] + ( ( $c1 !== $c2 ) ? 1 : 0 );
80 $currentRow[] = min( $insertions, $deletions, $substitutions );
82 $prevRow = $currentRow;
85 return $prevRow[$length2];