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); 
  • It is necessary to clarify the condition, the longest part in these words is aaa , where did it come from - оо ? - Alexey Shatrov
  • "oo" is the second matching part. I need to bring out both of these parts. And displays only one matching part - Andrii Shchur
  • Do I understand correctly that you need to print 2 or more identical letters standing side by side and present in all words? - Alexey Shatrov

2 answers 2

In this example, duplicate letters will be found next to each other, starting with two or more matches, and provided that these matches are in more than one word.

 <?php $text = 'Добрый ввеер, еесть код котооорый вывооодит...'; var_dump( check_matches($text) ); function check_matches($text, $array = [], $result = []) { foreach (explode(' ', $text) as $word) { preg_match_all('~(\pL)\1+~u', $word, $arr); !$arr[0] ?: $array[] = $arr[0]; } do { $first_array = array_shift($array); foreach ($array as $arr) { foreach (array_intersect($first_array, $arr) as $val) { $result[] = $val; } } } while (count($array)); return array_unique($result); } 

Result:

 array (size=3) 0 => string 'ее' (length=4) 1 => string 'ооо' (length=6) 
  • Perfect solution! The question is, when in a few words in a row there is a match on repeated letters, the output goes as one line $ text = 'Vaasiooo kaatnalooo'; array (1) {[0] => string (5) "aaooo"} - Andrii Shchur
  • @AndriiShchur corrected his answer. - Edward
  • Super. Thank! - Andrii Shchur
  • @AndriiShchur if my answer suits you, you can check it out. - Edward

Good time of day. Honestly it was too lazy to delve into the code, but through regular expressions it is solved quite simply. If I understood correctly

 $array = array( 'Argoolaaa', 'Banaaanoo' ); $pattern = "#(.)\\1{1,}#m"; foreach ($array as $el) { $mattches = []; preg_match_all($pattern, $el, $mattches); foreach ($mattches[0] as $f) { echo $f . PHP_EOL; } } 
  • This code doesn't work for me - Andrii Shchur
  • What does the script produce? - Evgeny Gavrilov
  • Sorry, my mistake. Works. Only this code displays all matching parts in order. "oo aaa aaa oo" - Andrii Shchur
  • And then already at your discretion, you can choose a unique or something else. For example, you can collect all values ​​into an array of $res and select unique data from there. $res = array_unique($res) The main goal is achieved, it is in words the choice of long parts in a word. - Evgeny Gavrilov