90 public static function levenshtein( $str1, $str2, $length1, $length2 ) {
91 if ( $length1 === 0 ) {
94 if ( $length2 === 0 ) {
97 if ( $str1 === $str2 ) {
101 $bytelength1 = strlen( $str1 );
102 $bytelength2 = strlen( $str2 );
103 if ( $bytelength1 === $length1 && $bytelength1 <= 255
104 && $bytelength2 === $length2 && $bytelength2 <= 255
109 $prevRow = range( 0, $length2 );
110 for ( $i = 0; $i < $length1; $i++ ) {
112 $currentRow[0] = $i + 1;
113 $c1 = mb_substr( $str1, $i, 1 );
114 for ( $j = 0; $j < $length2; $j++ ) {
115 $c2 = mb_substr( $str2, $j, 1 );
116 $insertions = $prevRow[$j + 1] + 1;
117 $deletions = $currentRow[$j] + 1;
118 $substitutions = $prevRow[$j] + ( ( $c1 !== $c2 ) ? 1 : 0 );
119 $currentRow[] = min( $insertions, $deletions, $substitutions );
121 $prevRow = $currentRow;
124 return $prevRow[$length2];