3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function snake($k,$y,$str1,$str2){ $x = $y - $k; while($x < mb_strlen($str1) && $y < mb_strlen($str2) && $str1[$x] == $str2[$y]){ $x++; $y++; } return $y; } function edit_distance_onp($str1, $str2){ $s1 = mb_strlen($str1) > mb_strlen($str2) ? $str2 : $str1; $s2 = mb_strlen($str1) > mb_strlen($str2) ? $str1 : $str2; $fp = array(); $x = 0; $y = 0; $k = 0; $p = 0; $offset = mb_strlen($s1) + 1; $delta = mb_strlen($s2) - mb_strlen($s1); for ($i = 0; $i < 9; $i++) $fp[$i] = -1; for ($p = 0; $fp[$delta + $offset] != mb_strlen($s2); $p++) { for($k = -$p; $k < $delta; $k++) $fp[$k + $offset] = snake($k, max($fp[$k-1+$offset] + 1, $fp[$k+1+$offset]), $s1, $s2); for($k = $delta + $p; $k > $delta; $k--) $fp[$k + $offset] = snake($k, max($fp[$k-1+$offset] + 1, $fp[$k+1+$offset]), $s1, $s2); $fp[$delta + $offset] = snake($delta, max($fp[$delta-1+$offset] + 1, $fp[$delta+1+$offset]), $s1, $s2); } return $delta + ($p - 1) * 2; } $cha1 = "aaa"; $cha2 = "bbb"; echo edit_distance_onp($cha1,$cha2); ?>
Output for 4.3.0 - 7.1.0
6