3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function split_preg($str) { return preg_split('//u', $str, -1); } function getFormattedString($str) { return implode('', explode(' ', mb_strtolower($str, 'UTF-8'))); } function getOddLengthPalindromeArray($str) { $result = []; $l = 0; $r = -1; var_dump(array_filter(split_preg($str))); 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 Ea2RV
Output for 7.1.0 - 7.2.0
array(19) { [1]=> string(2) "а" [2]=> string(2) "р" [3]=> string(2) "г" [4]=> string(2) "е" [5]=> string(2) "н" [6]=> string(2) "т" [7]=> string(2) "и" [8]=> string(2) "н" [9]=> string(2) "а" [10]=> string(2) "м" [11]=> string(2) "а" [12]=> string(2) "н" [13]=> string(2) "и" [14]=> string(2) "т" [15]=> string(2) "н" [16]=> string(2) "е" [17]=> string(2) "г" [18]=> string(2) "р" [19]=> string(2) "а" } �ар����нт������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.4.10 - 7.0.20
array(19) { [1]=> string(2) "а" [2]=> string(2) "р" [3]=> string(2) "г" [4]=> string(2) "е" [5]=> string(2) "н" [6]=> string(2) "т" [7]=> string(2) "и" [8]=> string(2) "н" [9]=> string(2) "а" [10]=> string(2) "м" [11]=> string(2) "а" [12]=> string(2) "н" [13]=> string(2) "и" [14]=> string(2) "т" [15]=> string(2) "н" [16]=> string(2) "е" [17]=> string(2) "г" [18]=> string(2) "р" [19]=> string(2) "а" } Notice: Uninitialized string offset: -1 in /in/4HkWs on line 21 ар����нт������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.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/4HkWs on line 13
Process exited with code 255.