3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Sample data $data = [ 'Wellington New Zealand', 'Florida United States of America', 'Quebec Canada', 'Something Country XYZ (formally ABC)', ]; // Array of all possible countries $countries = [ 'United States of America', 'Canada', 'New Zealand', 'Country XYZ (formally ABC)', ]; // The begining and ending pattern delimiter for the RegEx $delim = '/'; /*************************************************** * Option 1 * * Simple but potentially flawed depending on data * **************************************************/ matchAndShowData($data, $countries, $delim, implode('|', $countries)); echo PHP_EOL, PHP_EOL; /******************************* * Option 2 * * Always escape, just in case * ******************************/ // This is an alternative form of the above, and is arguably overkill for this specific // situation, but I always run my code preg_quote, just in case. // This function will ensure that just in case a country has a RegEx modifier that it gets // properly escaped. $patternParts = array_map(fn(string $country) => preg_quote($country, $delim), $countries); // Implode the cleaned countries using the RegEx pipe operator which means "OR" matchAndShowData($data, $countries, $delim, implode('|', $patternParts)); function matchAndShowData(array $data, array $countries, string $delim, string $countryParts): void { $pattern = "^(?<region>.*?) (?<country>$countryParts)$"; foreach($data as $d) { if(preg_match($delim . $pattern . $delim, $d, $matches)){ echo sprintf('%1$s, %2$s', $matches['region'], $matches['country']), PHP_EOL; } else { echo 'NO MATCH: ' . $d, PHP_EOL; } } }

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.0110.00623.99
8.4.110.0040.00520.73
8.4.100.0130.00718.71
8.4.90.0100.00920.77
8.4.80.0050.00518.73
8.4.70.0100.01420.87
8.4.60.0110.00918.60
8.4.50.0120.00920.50
8.4.40.0130.00717.77
8.4.30.0120.00820.47
8.4.20.0040.01819.66
8.4.10.0030.00622.13
8.3.250.0070.00317.32
8.3.240.0050.00417.16
8.3.230.0140.00616.88
8.3.220.0090.00918.88
8.3.210.0120.00718.60
8.3.200.0030.00616.86
8.3.190.0130.00617.16
8.3.180.0080.00818.74
8.3.170.0090.00616.86
8.3.160.0160.00316.97
8.3.150.0120.00617.36
8.3.140.0060.00320.71
8.3.130.0030.00618.42
8.3.120.0130.00620.80
8.3.110.0050.00520.94
8.3.100.0040.00424.06
8.3.90.0050.00326.77
8.3.80.0030.00716.88
8.3.70.0100.01017.00
8.3.60.0120.00316.63
8.3.50.0150.00320.30
8.3.40.0070.01020.30
8.3.30.0130.00318.84
8.3.20.0050.00324.18
8.3.10.0080.00024.66
8.3.00.0000.00826.16
8.2.290.0110.00720.49
8.2.280.0090.01018.85
8.2.270.0110.00717.03
8.2.260.0040.00416.72
8.2.250.0080.00816.70
8.2.240.0120.00319.00
8.2.230.0080.00822.58
8.2.220.0080.00037.54
8.2.210.0090.00626.77
8.2.200.0060.00316.88
8.2.190.0140.00717.00
8.2.180.0110.00425.92
8.2.170.0070.01019.18
8.2.160.0080.00822.96
8.2.150.0050.00225.66
8.2.140.0030.00524.66
8.2.130.0040.00426.16
8.2.120.0000.00821.03
8.2.110.0030.00620.39
8.2.100.0040.00817.84
8.2.90.0050.00318.03
8.2.80.0030.00617.97
8.2.70.0050.00317.88
8.2.60.0050.00317.88
8.2.50.0000.00817.88
8.2.40.0050.00517.88
8.2.30.0000.00718.24
8.2.20.0040.00422.13
8.2.10.0000.00718.08
8.2.00.0000.00819.49
8.1.330.0090.01022.06
8.1.320.0120.00716.68
8.1.310.0040.01218.09
8.1.300.0110.00716.61
8.1.290.0000.00918.88
8.1.280.0060.01025.92
8.1.270.0090.00023.95
8.1.260.0030.00526.35
8.1.250.0080.00028.09
8.1.240.0140.00722.27
8.1.230.0040.00822.23
8.1.220.0050.00317.79
8.1.210.0030.00618.91
8.1.200.0000.00917.60
8.1.190.0000.00817.47
8.1.180.0030.00718.10
8.1.170.0030.00517.64
8.1.160.0090.00019.07
8.1.150.0070.00018.83
8.1.140.0000.00817.57
8.1.130.0050.00218.96
8.1.120.0040.00417.53
8.1.110.0000.00817.65
8.1.100.0040.00417.56
8.1.90.0030.00617.54
8.1.80.0120.00817.64
8.1.70.0120.00317.62
8.1.60.0150.00517.74
8.1.50.0080.01217.66
8.1.40.0100.01017.68
8.1.30.0070.00717.72
8.1.20.0110.00717.71
8.1.10.0150.00417.72
8.1.00.0150.00217.67
8.0.300.0040.00419.95
8.0.290.0060.00316.88
8.0.280.0000.00818.48
8.0.270.0030.00318.21
8.0.260.0000.00718.43
8.0.250.0030.00317.03
8.0.240.0000.00817.15
8.0.230.0000.00717.09
8.0.220.0140.00317.04
8.0.210.0120.00317.06
8.0.200.0120.00416.97
8.0.190.0070.00717.13
8.0.180.0090.00417.00
8.0.170.0080.00716.96
8.0.160.0100.00316.98
8.0.150.0060.00616.96
8.0.140.0040.00817.05
8.0.130.0040.00816.96
8.0.120.0110.00417.05
8.0.110.0120.00017.04
8.0.100.0120.00317.00
8.0.90.0090.00716.92
8.0.80.0120.00317.07
8.0.70.0150.00016.81
8.0.60.0130.00217.02
8.0.50.0150.00016.93
8.0.30.0090.00617.07
8.0.20.0080.00817.02
8.0.10.0160.00017.20
7.4.330.0060.00015.55
7.4.320.0000.00616.66
7.4.300.0110.00316.85
7.4.290.0090.01116.63
7.4.280.0080.00816.83
7.4.270.0120.00416.74
7.4.260.0050.00916.82
7.4.250.0090.00616.72
7.4.240.0130.00016.84
7.4.230.0140.00016.66
7.4.220.0070.00316.86
7.4.210.0000.01316.80
7.4.200.0100.00716.66
7.4.190.0100.00316.70
7.4.180.0110.00216.69
7.4.160.0050.01016.85
7.4.150.0150.00016.68
7.4.140.0130.00316.67
7.4.130.0090.00316.66
7.4.120.0130.00016.61
7.4.110.0100.00316.63
7.4.100.0090.00516.79
7.4.90.0050.01016.83
7.4.80.0070.00716.58
7.4.70.0080.00616.58
7.4.60.0060.00916.63
7.4.50.0170.00016.71
7.4.40.0080.00816.57
7.4.30.0080.00816.58
7.4.20.0090.00716.68
7.4.10.0120.00316.59
7.4.00.0100.00316.66

preferences:
31.77 ms | 403 KiB | 5 Q