3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getSimilarSubstring($a, $b) { if(!strlen($a) || !strlen($b)) { return ''; } if(false!==strpos($a, $b)) { return $b; } $length = strlen($b); for($i=$length-1; $i>0; $i--) { for($j=0; $j<$length-$i; $j++) { echo(sprintf('Current[%s][%s] = %s'.PHP_EOL, $i, $j, substr($b, $j, $i))); if($result = getSimilarSubstring($a, substr($b, $j, $i))) { return $result; } } } return ''; } $word_a = "superman"; $word_b = "stermastein"; //$word_a = 'afk'; //$word_b = 'fk'; var_dump(getSimilarSubstring($word_a, $word_b));

preferences:
40.34 ms | 402 KiB | 5 Q