Good evening, there is a code that outputs the longest part in a nutshell, in this case "aaa". Please tell me how it is possible to make them in the case of several such parts. Specifically here "aaa" and "oo"
$array = array( 'Argoolaaa', 'Banaaanoo' ); function longestCommonPart($words) { $words = array_map('strtolower', array_map('trim', $words)); $sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;'); usort($words, $sort_by_strlen); $longestCommonPart = array(); $shortest_string = str_split(array_shift($words)); while (sizeof($shortest_string)) { array_unshift($longestCommonPart, ''); foreach ($shortest_string as $ci => $char) { foreach ($words as $wi => $word) { if (!strstr($word, $longestCommonPart[0] . $char)) { break 2; } } $longestCommonPart[0] .= $char; } array_shift($shortest_string); } usort($longestCommonPart, $sort_by_strlen); return array_pop($longestCommonPart); } echo longestCommonPart($array);
aaa, where did it come from -оо? - Alexey Shatrov