3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = 'RA(B(CCC)B(CC))PA(BB(C))D'; abstract class Component { public $letter; public function __construct($letter) { $this->letter = $letter; } public function dump($level = 0) { echo str_repeat("&emsp;", $level).$this->letter."\n"; } } class Leaf extends Component {} class Node extends Component { /** @var Component[] */ private $items = []; private static function findComponentsFromInput($input) { $numOpenBrackets = false; $numClosedBrackets = false; $trimPositions = []; $lastTrimPosition = 0; for ($currentCharNumber = 0, $strLen = strlen($input); $currentCharNumber < $strLen; $currentCharNumber++) { if ($input{$currentCharNumber} == '(') { $numOpenBrackets = (int)$numOpenBrackets + 1; } elseif ($input{$currentCharNumber} == ')') { $numClosedBrackets = (int)$numClosedBrackets + 1; } $isLeaf = $numOpenBrackets === false && $numClosedBrackets === false && (!isset($input{$currentCharNumber + 1}) || $input{$currentCharNumber + 1} != '('); if ($isLeaf) { $trimPositions[] = [ "type" => 'leaf', "start_trim" => $lastTrimPosition, "trim_length" => $currentCharNumber - $lastTrimPosition + 1 ]; $lastTrimPosition = $currentCharNumber + 1; } $isNode = is_int($numOpenBrackets) && is_int($numClosedBrackets) && $numOpenBrackets - $numClosedBrackets == 0; if ($isNode) { $numOpenBrackets = false; $numClosedBrackets = false; $trimPositions[] = [ "type" => "node", "start_trim" => $lastTrimPosition, "trim_length" => $currentCharNumber - $lastTrimPosition + 1 ]; $lastTrimPosition = $currentCharNumber + 1; } } $resultComponents = []; foreach ($trimPositions as $trimPosition) { $resultComponents[] = [ "type" => $trimPosition["type"], "data" => substr($input, $trimPosition["start_trim"], $trimPosition["trim_length"]) ]; } return $resultComponents; } public function createComponentsFromInput($dataString) { $components = self::findComponentsFromInput($dataString); foreach($components as $component) { if($component['type'] == 'leaf') { $this->addItem(new Leaf($component['data'])); } elseif($component['type'] == 'node') { preg_match('~(\w)*\((.+)\)~', $component['data'], $matches); if(!isset($matches[1]) || !isset($matches[2])) { echo "Wrong input format"; exit; } $newNode = new Node($matches[1]); $newNode->createComponentsFromInput($matches[2]); $this->addItem($newNode); } } return $this; } public function addItem(Component $item) { $this->items[] = $item; } public function dump($level = 0) { if($this->letter == 'root') { $newLevel = $level; } else { parent::dump($level); $newLevel = $level + 1; } foreach($this->items as $item) { $item->dump($newLevel); } return $this; } } $root = new Node("root"); $root->createComponentsFromInput($input)->dump();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/K246b on line 35
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/K246b on line 35
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/K246b on line 35 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/K246b on line 37 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/K246b on line 41 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/K246b on line 41 R A &emsp;B &emsp;&emsp;C &emsp;&emsp;C &emsp;&emsp;C &emsp;B &emsp;&emsp;C &emsp;&emsp;C P A &emsp;B &emsp;B &emsp;&emsp;C D
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33
R A &emsp;B &emsp;&emsp;C &emsp;&emsp;C &emsp;&emsp;C &emsp;B &emsp;&emsp;C &emsp;&emsp;C P A &emsp;B &emsp;B &emsp;&emsp;C D

preferences:
161.81 ms | 402 KiB | 170 Q