3v4l.org

run code in 300+ PHP versions simultaneously
<?php function parse($pattern) { $matches = ''; $variables = array(); $pos = 0; $reg = '#'; //It seems that regex must start and end with a delimiter $nextText = ''; if($pattern == '/') { $reg = '#^[\/]+$#'; return ['variables' => '', 'regex' => $reg]; } //Check if generated regexes are stored, if so it skips the whole process /*if(apc_exists($pattern)) { $cacheI = apc_fetch($pattern); return $cacheI; }*/ //Extracts the variables enclosed in {} preg_match_all('#\{\w+\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); //Puts each variable in array //Uses the text before and after to create a regex for the rest of the pattern - $precedingText, $nextText //If no wildcard is detected in the path it splits it into segments and compiles are regex foreach ($matches as $match) { $varName = substr($match[0][0], 1, -1); $precedingText = substr($pattern, $pos, $match[0][1] - $pos); $pos = $match[0][1] + strlen($match[0][0]); $nxt = $pos - strlen($pattern); if($nxt == 0) $nxt = strlen($pattern); $nextText = substr($pattern, $nxt); $precSegments = explode('/', $precedingText); $precSegments = array_splice($precSegments, 1); //Pulls a regex from the preeceding segment, each variable segment is replaced with '\/[a-zA-Z0-9]+' if(strlen($precedingText) > 1) { foreach($precSegments as $key => $value) { $reg .= '\/'; $reg .= $value; } $reg .= '[a-zA-Z0-9]+'; } else { $reg .= '\/[a-zA-Z0-9]+'; } $nextText = str_replace('/', '\/', $nextText); if(is_numeric($varName)) { throw new \Exception('Argument cannot be a number'); } if (in_array($varName, $variables)) { throw new \Exception(sprintf('More then one occurrence of variable name "%s".', $varName)); } $variables[] = $varName; } //If no variable names, wildcards are found in pattern : /hello/static/path it will replace it with \/hello\/static\/path if(count($matches) < 1 && $pattern != '/') { $reg .= str_replace('/', '\/', $pattern); } $reg = $reg . $nextText; $reg .= '#'; //apc_store($pattern, ['variables' => $variables, 'regex' => $reg]); return ['variables' => $variables, 'regex' => $reg]; } $data = parse('hi/{p1}/cicki/{p2}'); echo 'parse: '.var_export($data, 1);

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.4.120.0120.00724.24
8.4.110.0120.00719.12
8.4.100.0120.00918.90
8.4.90.0060.00220.75
8.4.80.0050.00418.91
8.4.70.0100.00618.27
8.4.60.0140.00718.84
8.4.50.0110.01117.90
8.4.40.0120.00917.84
8.4.30.0070.00420.57
8.4.20.0070.01419.73
8.4.10.0030.00622.38
8.3.250.0110.00819.19
8.3.240.0100.01017.30
8.3.230.0140.00617.02
8.3.220.0060.00119.20
8.3.210.0100.01017.02
8.3.200.0050.00416.86
8.3.190.0040.00519.20
8.3.180.0120.00718.52
8.3.170.0040.01519.06
8.3.160.0160.00316.66
8.3.150.0100.01017.12
8.3.140.0060.00917.32
8.3.130.0080.00018.39
8.3.120.0030.01319.30
8.3.110.0090.00620.94
8.3.100.0100.00016.82
8.3.90.0030.00726.77
8.3.80.0060.00617.00
8.3.70.0040.01116.86
8.3.60.0060.00916.75
8.3.50.0110.00418.78
8.3.40.0100.00719.09
8.3.30.0100.00319.21
8.3.20.0040.00424.18
8.3.10.0030.00524.66
8.3.00.0000.00826.16
8.2.290.0110.00920.23
8.2.280.0120.00918.51
8.2.270.0000.01617.51
8.2.260.0090.00918.72
8.2.250.0060.00316.94
8.2.240.0000.00817.39
8.2.230.0040.00422.58
8.2.220.0030.00624.06
8.2.210.0070.00026.77
8.2.200.0060.00316.75
8.2.190.0100.01016.63
8.2.180.0000.01425.92
8.2.170.0070.00722.96
8.2.160.0080.00622.96
8.2.150.0000.00825.66
8.2.140.0000.00724.66
8.2.130.0040.00426.16
8.2.120.0030.00519.55
8.2.110.0060.00322.25
8.2.100.0060.00618.03
8.2.90.0000.00918.03
8.2.80.0060.00317.97
8.2.70.0090.00017.75
8.2.60.0030.00617.93
8.2.50.0000.00818.07
8.2.40.0080.00018.28
8.2.30.0080.00018.13
8.2.20.0020.00520.61
8.2.10.0080.00018.19
8.2.00.0040.00419.43
8.1.330.0100.00921.91
8.1.320.0120.00818.23
8.1.310.0030.00618.34
8.1.300.0060.00318.66
8.1.290.0030.00630.84
8.1.280.0100.00725.92
8.1.270.0000.00823.99
8.1.260.0030.00526.35
8.1.250.0040.00428.09
8.1.240.0060.00318.85
8.1.230.0090.00321.09
8.1.220.0000.00817.74
8.1.210.0030.00619.00
8.1.200.0060.00317.60
8.1.190.0030.00617.48
8.1.180.0050.00318.10
8.1.170.0040.00418.91
8.1.160.0070.00019.02
8.1.150.0000.00718.71
8.1.140.0030.00617.64
8.1.130.0000.00719.02
8.1.120.0080.00317.62
8.1.110.0030.00517.51
8.1.100.0040.00417.65
8.1.90.0040.00417.68
8.1.80.0050.00217.64
8.1.70.0040.00417.53
8.1.60.0030.00717.64
8.1.50.0140.00417.69
8.1.40.0120.00617.59
8.1.30.0120.00617.78
8.1.20.0130.00517.73
8.1.10.0120.00617.74
8.1.00.0160.00317.63
8.0.300.0070.00020.02
8.0.290.0050.00317.18
8.0.280.0040.00420.24
8.0.270.0030.00518.20
8.0.260.0000.00718.52
8.0.250.0030.00317.04
8.0.240.0030.00317.16
8.0.230.0000.00817.13
8.0.220.0070.00017.07
8.0.210.0030.00517.05
8.0.200.0000.00617.03
8.0.190.0020.00817.02
8.0.180.0060.00417.08
8.0.170.0050.01017.05
8.0.160.0120.00317.06
8.0.150.0100.00516.87
8.0.140.0130.00117.05
8.0.130.0110.00416.94
8.0.120.0090.00616.98
8.0.110.0080.00717.08
8.0.100.0130.00217.10
8.0.90.0080.00716.98
8.0.80.0130.00317.00
8.0.70.0100.00617.02
8.0.60.0070.00816.94
8.0.50.0050.01116.93
8.0.30.0080.00816.99
8.0.20.0110.00517.15
8.0.10.0090.00817.07
7.4.330.0000.00515.55
7.4.320.0030.00316.84
7.4.300.0030.00316.66
7.4.290.0080.00316.74
7.4.280.0150.00216.83
7.4.270.0140.00316.82
7.4.260.0050.00216.78
7.4.250.0030.00616.77
7.4.240.0090.00516.73
7.4.230.0120.00616.72
7.4.220.0100.00516.69
7.4.210.0100.00616.79
7.4.200.0060.00516.84
7.4.190.0090.00716.82
7.4.180.0140.00416.90
7.4.160.0080.00716.85
7.4.150.0090.00416.75
7.4.140.0090.00516.74
7.4.130.0080.00716.77
7.4.120.0100.00516.66
7.4.110.0110.00416.80
7.4.100.0090.00616.81
7.4.90.0140.00016.66
7.4.80.0070.00316.73
7.4.70.0110.00316.77
7.4.60.0090.00616.65
7.4.50.0110.00216.76
7.4.40.0080.00616.58
7.4.30.0140.00016.72
7.4.20.0100.00416.65
7.4.10.0080.00516.67
7.4.00.0090.00516.65

preferences:
27.16 ms | 403 KiB | 5 Q