3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Token { public function __toString() { return get_called_class(); } } class ParensOpenToken extends Token {} class ParensCloseToken extends Token {} class SemicolonToken extends Token {} class ValueToken extends Token { public $value = ''; public function __toString() { return parent::__toString() . '("' . $this->value . '")'; } } class OtherToken extends Token { public $value = ''; public function __toString() { return parent::__toString() . '("' . $this->value . '")'; } } class Tokenizer { public function tokenize($string) { $tokensList = [ '[ \t\n]' => OtherToken::class, '\\{' => ParensOpenToken::class, '\\}' => ParensCloseToken::class, ';' => SemicolonToken::class, '[a-zA-Zа-яА-Я\-_0-9]+' => ValueToken::class ]; $tokens = []; while (mb_strlen($string) > 0) { $matched = false; foreach ($tokensList as $pattern => $token) { if (preg_match('/^(' . $pattern . ')/', $string, $out) !== FALSE) { if (isset($out[1])) { $token = new $token(); if ($token instanceof ValueToken && !is_null($out[1])) { $token->value = $out[1]; } $tokens[] = $token; $string = mb_substr($string, mb_strlen($out[1])); $matched = true; break; } } } if (!$matched) { $value = mb_substr($string, 0, 1); $otherToken = new OtherToken(); $otherToken->value = $value; $string = mb_substr($string, 1); } } return $tokens; } } class Parser { private $_tokens = []; private $_index = 0; private function isTokensAvailable() { return $this->_index < count($this->_tokens); } private function peekCurrentToken() { return $this->_tokens[$this->_index]; } private function popCurrentToken() { return $this->_tokens[$this->_index++]; } private function parsePrimary() { $peek = $this->peekCurrentToken(); if ($peek instanceof ParensOpenToken) { return $this->parseParens(); } elseif ($peek instanceof ValueToken) { return $this->parseValue(); } else { throw new \Exception('Parser error: unexpected character at index ' . $this->_index); return null; } } private function parseParens() { $token = $this->popCurrentToken(); if (!($token instanceof ParensOpenToken)) { throw new \Exception('Parser error: expected {'); } $expressions = []; while (true) { $expression = $this->parsePrimary(); $expressions[] = $expression; $token = $this->peekCurrentToken(); if ($token instanceof ParensCloseToken) { $this->popCurrentToken(); return $expressions; } elseif (!($token instanceof SemicolonToken)) { throw new \Exception('Parser error: expected semicolon after object in array'); } else { $this->popCurrentToken(); } } return $expressions; } private function parseValue() { $token = $this->popCurrentToken(); if (!($token instanceof ValueToken)) { throw new \Exception('Parser error: expected value token'); } return $token->value; } public function parse($tokens) { $result = []; $this->_tokens = $tokens; $this->_index = 0; while ($this->isTokensAvailable()) { $result[] = $this->parsePrimary(); } return $result; } } $tokenizer = new Tokenizer(); $parser = new Parser(); $string = '{{param1;{param2_array};param3;param4};{param5;{param6_array};param7;param8}}'; $tokens = $tokenizer->tokenize($string); $result = $parser->parse($tokens); print_r($result); $string2 = '{{param1;{param2_array;param2_array;param2_array};param3;param4}}'; $tokens2 = $tokenizer->tokenize($string2); $result2 = $parser->parse($tokens2); print_r($result2);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0150.00616.88
8.3.60.0120.00318.68
8.3.50.0150.00916.92
8.3.40.0130.01019.17
8.3.30.0080.00818.88
8.3.20.0050.00321.93
8.3.10.0040.00423.51
8.3.00.0080.00020.90
8.2.180.0030.01525.92
8.2.170.0150.00022.96
8.2.160.0170.00020.35
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0040.00418.22
8.2.120.0030.00526.35
8.2.110.0050.00519.14
8.2.100.0060.00618.28
8.2.90.0050.00519.59
8.2.80.0030.00619.36
8.2.70.0080.00017.75
8.2.60.0040.00418.13
8.2.50.0030.00518.00
8.2.40.0050.00319.59
8.2.30.0030.00619.39
8.2.20.0040.00418.12
8.2.10.0000.00819.48
8.2.00.0070.00319.39
8.1.280.0100.00725.92
8.1.270.0040.00418.98
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0090.00022.26
8.1.230.0090.00321.04
8.1.220.0080.00017.78
8.1.210.0060.00319.02
8.1.200.0030.00617.60
8.1.190.0000.00917.61
8.1.180.0040.00418.10
8.1.170.0090.00018.75
8.1.160.0000.00818.97
8.1.150.0080.00019.10
8.1.140.0000.00820.96
8.1.130.0030.00518.92
8.1.120.0040.00417.69
8.1.110.0040.00817.67
8.1.100.0060.00317.72
8.1.90.0000.00917.61
8.1.80.0050.00317.59
8.1.70.0030.00617.70
8.1.60.0000.00817.86
8.1.50.0040.00417.88
8.1.40.0000.00917.83
8.1.30.0030.00617.93
8.1.20.0000.00917.98
8.1.10.0040.00417.89
8.1.00.0030.00617.73
8.0.300.0040.00420.20
8.0.290.0030.00617.00
8.0.280.0040.00418.69
8.0.270.0000.00717.24
8.0.260.0000.00718.50
8.0.250.0000.00717.34
8.0.240.0030.00317.23
8.0.230.0000.00717.31
8.0.220.0070.00017.30
8.0.210.0070.00317.24
8.0.200.0030.00317.21
8.0.190.0030.00617.32
8.0.180.0060.00317.30
8.0.170.0060.00317.29
8.0.160.0040.00417.16
8.0.150.0000.00817.11
8.0.140.0050.00317.25
8.0.130.0000.00613.64
8.0.120.0040.00417.24
8.0.110.0050.00317.19
8.0.100.0030.00517.11
8.0.90.0040.00417.21
8.0.80.0100.00917.30
8.0.70.0000.00817.15
8.0.60.0080.00017.10
8.0.50.0020.00517.06
8.0.30.0110.00917.29
8.0.20.0110.00717.40
8.0.10.0040.00417.35
8.0.00.0140.00417.06
7.4.330.0000.00515.55
7.4.320.0030.00316.95
7.4.300.0000.00716.80
7.4.290.0040.00416.89
7.4.280.0030.00516.74
7.4.270.0070.00016.91
7.4.260.0040.00416.90
7.4.250.0050.00316.73
7.4.240.0000.00716.96
7.4.230.0000.00716.81
7.4.220.0090.00017.02
7.4.210.0050.01316.79
7.4.200.0000.00716.99
7.4.160.0120.00616.83
7.4.140.0100.01017.86
7.4.130.0120.01216.61
7.4.120.0120.00816.92
7.4.110.0130.00416.82
7.4.100.0180.00316.94
7.4.90.0150.00316.79
7.4.80.0140.01119.39
7.4.70.0100.00716.86
7.4.60.0140.00316.62
7.4.50.0060.00616.43
7.4.40.0000.01816.92
7.4.00.0080.00815.19
7.3.330.0000.00613.54
7.3.320.0060.00013.66
7.3.310.0030.00516.69
7.3.300.0030.00316.62
7.3.290.0030.00316.56
7.3.280.0080.00916.63
7.3.260.0090.00916.81
7.3.240.0130.00616.80
7.3.230.0070.01016.79
7.3.210.0100.01016.64
7.3.200.0140.00416.56
7.3.190.0060.01216.65
7.3.180.0030.01316.96
7.3.170.0060.01216.82
7.3.160.0090.00816.81
7.3.120.0040.01515.03
7.3.110.0040.01514.85
7.3.100.0070.01014.95
7.3.90.0070.00715.32
7.3.80.0040.00815.31
7.3.70.0040.00815.05
7.3.60.0030.01315.14
7.3.50.0100.00714.97
7.3.40.0000.01215.21
7.3.30.0060.01015.06
7.3.20.0090.00617.09
7.3.10.0160.00915.71
7.3.00.0120.00715.72
7.2.330.0140.00316.93
7.2.320.0090.00916.76
7.2.310.0070.01016.92
7.2.300.0130.00416.70
7.2.290.0110.01116.95
7.2.250.0100.01115.29
7.2.240.0110.00715.40
7.2.230.0100.00715.26
7.2.220.0130.00915.50
7.2.210.0030.00915.32
7.2.200.0030.01315.31
7.2.190.0030.01315.09
7.2.180.0060.00815.33
7.2.170.0070.00715.34
7.2.130.0220.00615.27
7.2.120.0770.01415.21
7.2.110.0160.00915.18
7.2.100.0480.00415.14
7.2.90.0240.01115.45
7.2.80.0340.00415.24
7.2.70.0130.00715.37
7.2.60.0190.01515.10
7.2.50.4890.01015.32
7.2.40.0450.00715.30
7.2.30.2870.01315.32
7.2.20.0300.00715.03
7.2.10.0300.01215.25
7.2.00.0050.01715.48
7.1.330.0040.01215.97
7.1.320.0050.00516.17
7.1.310.0080.00615.90
7.1.300.0000.01516.02
7.1.290.0080.00415.83
7.1.280.0000.01415.77
7.1.270.0090.00615.93
7.1.260.0060.00615.82
7.1.250.0330.01014.01

preferences:
68.11 ms | 401 KiB | 5 Q