3v4l.org

run code in 300+ PHP versions simultaneously
<?php function str_intersection($str1, $str2) { [$long, $short] = strlen($str1) > strlen($str2) ? [$str1, $str2] : [$str2, $str1]; $shortLength = strlen($short); for ($length = $shortLength; $length > 0; $length--) { for ($offset = 0; $offset < $shortLength - 1; $offset++) { if (strpos($long, substr($short, $offset, $length)) !== false) return $length; } } return 0; } $str1 = "lorem ipsum"; $str2 = "rem"; echo str_intersection($str1, $str2) . PHP_EOL; // Expected result: 3 $str2 = "xzy"; echo str_intersection($str1, $str2) . PHP_EOL; // Expected result: 0
Output for 8.0.25, 8.1.23 - 8.1.28, 8.2.10 - 8.2.18, 8.3.0 - 8.3.6
3 0

preferences:
75.65 ms | 402 KiB | 28 Q