3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getFormattedString($str) { return implode('', explode(' ', mb_strtolower($str, 'UTF-8'))); } function getOddLengthPalindromeArray($str) { $result = []; $l = 0; $r = -1; for ($i = 0, $n = mb_strlen($str, 'UTF-8'); $i < $n; $i++) { $k = ($i > $r ? 1 : min($result[$l + $r - $i], $r - $i)); echo $str[$i - $k]; while ($i - $k >= 0 && $i + $k < $n && $str[$i - $k] == $str[$i + $k]) { $k++; } $result[$i] = $k; if ($i + $k - 1 > $r) { $l = $i - $k + 1; $r = $i + $k - 1; } } return $result; } function getEvenLengthPalindromeArray($str) { $result = []; $l = 0; $r = -1; for ($i = 0, $n = mb_strlen($str, 'UTF-8'); $i < $n; $i++) { $k = ($i > $r ? 0 : min($result[$l + $r - $i - 1], $r - $i)); while ($i - $k >= 0 && $i + $k + 1 < $n && $str[$i - $k] == $str[$i + $k + 1]) { $k++; } $result[$i] = $k; if ($i + $k > $r) { $l = $i - $k + 1; $r = $i + $k; } } return $result; } $formattedString = getFormattedString("Аргентина манит негра"); $oddPalindromeArray = getOddLengthPalindromeArray($formattedString); $evenPalindromeArray = getEvenLengthPalindromeArray($formattedString); $maxOddValue = max($oddPalindromeArray); $maxEvenValue = max($evenPalindromeArray); $maxOddPalindromeLength = $maxOddValue * 2 - 1; $maxEvenPalindromeLength = $maxEvenValue * 2; if ($maxOddPalindromeLength > $maxEvenPalindromeLength && $maxOddValue !== 1) { $maxOddKey = array_search($maxOddValue, $oddPalindromeArray); var_dump($oddPalindromeArray); //var_dump(mb_substr($formattedString, $maxOddKey - $maxOddValue + 1, $maxOddPalindromeLength, 'UTF-8')); } elseif ($maxOddValue * 2 - 1 < $maxEvenValue * 2 && $maxEvenValue !== 0) { $maxEvenKey = array_search($maxEvenValue, $evenPalindromeArray); var_dump(mb_substr($formattedString, $maxEvenKey - $maxEvenValue, $maxEvenPalindromeLength, 'UTF-8')); } else { var_dump($formattedString[0]); }
based on 0Zro5
Output for 7.1.0 - 7.2.0
�ар����нт������array(19) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(2) [6]=> int(1) [7]=> int(2) [8]=> int(1) [9]=> int(1) [10]=> int(1) [11]=> int(1) [12]=> int(1) [13]=> int(2) [14]=> int(1) [15]=> int(2) [16]=> int(1) [17]=> int(2) [18]=> int(1) }
Output for 5.5.0 - 7.0.20
Notice: Uninitialized string offset: -1 in /in/R1Hd0 on line 15 ар����нт������array(19) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(2) [6]=> int(1) [7]=> int(2) [8]=> int(1) [9]=> int(1) [10]=> int(1) [11]=> int(1) [12]=> int(1) [13]=> int(2) [14]=> int(1) [15]=> int(2) [16]=> int(1) [17]=> int(2) [18]=> int(1) }