3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Task 2: Parse the string * * Parse the $input string and build object tree that represents this structure: * https://drive.google.com/file/d/0BwhWcFzxN6_mZ2hkQlhZTnRrMDA/view?usp=sharing * Use Node class to build the tree and dump it to output * * Realize Node::dump() function and use it to output the tree in format: * A * B * C * C * C * B * C * C * A * B * B * C */ $input = 'A(B(CCC)B(CC))A(BB(C))'; class Node { public $letter; private $items = []; public function __construct($letter) { $this->letter = $letter; } public function addItem($item) { $this->items[] = $item; } public function dump($level = 0) { // write your code here... } } function createTree(string $input) { $jsonInput = $input; $jsonInput = str_replace(['(', ')'], ['[', ']'], $jsonInput); $jsonInput = preg_replace('~(\w)\[~', '"${1}":\[', $jsonInput); echo $jsonInput; } createTree($input);
based on LUdKn
Output for hhvm-3.15.4, 7.0.0 - 7.3.0beta1
"A":\["B":\[CCC]"B":\[CC]]"A":\[B"B":\[C]]
Output for 5.6.0 - 5.6.30
Catchable fatal error: Argument 1 passed to createTree() must be an instance of string, string given, called in /in/8mgef on line 50 and defined in /in/8mgef on line 42
Process exited with code 255.