3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * PHP library for working with URI's. Requires * PHP 5.3.7 or later. Replaces and extends PHP's * parse_url() * * Based on P Guardiario's original work * * @author Nicholas Jordon * @copyright 2014 Nicholas Jordon - All Rights Reserved * @license http://opensource.org/licenses/MIT * @version 0.1.0 */ /** * PHP URI */ class uri { /*** Variables ***/ public $input; public $scheme; public $protocol; public $scheme_name; public $user; public $username; public $pass; public $password; public $host; public $fqdn; public $port; public $authority; public $path; public $query; public $fragment; public $error; public $error_msg; /*** Methods ***/ public function __construct($input) { $t = $this; $t->input = $input; $t->error = FALSE; $t->protocol = &$this->scheme; $t->username = &$this->user; $t->password = &$this->pass; $t->fqdn = &$this->host; if (!is_string($input)) { $t->error = TRUE; $t->error_msg = 'Input was not a string!'; $t->scheme = FALSE; $t->scheme_name = FALSE; $t->user = FALSE; $t->pass = FALSE; $t->host = FALSE; $t->port = FALSE; $t->authority = FALSE; $t->path = FALSE; $t->query = FALSE; $t->fragment = FALSE; } else { $this->parse($input); } } protected function parse($uri) { if ($this->error) { return FALSE; } $t = $this; $parsed = $t->_parse((string) $uri); if (empty($parsed)) { $t->error = TRUE; $t->error = 'Could not parse the input as a URI'; return $parsed; } $defaults = array( 'scheme' => '', 'scheme_name' => '', 'user' => '', 'pass' => '', 'host' => '', 'port' => '', 'authority' => '', 'path' => '', 'query' => '', 'fragment' => '' ); $values = $parsed + $defaults; if (!empty($values['scheme'])) { $t->scheme = $values['scheme'].'://'; } else { $t->scheme = ''; } $t->scheme_name = $values['scheme']; $t->user = $values['user']; $t->pass = $values['pass']; $t->host = $values['host']; $t->port = $values['port']; $t->path = $values['path']; $t->query = $values['query']; $t->fragment = $values['fragment']; $t->gen_authority(); } private function _parse($uri) { $uri = (string) $uri; if (!version_compare(PHP_VERSION, '5.4.7') >= 0) { if ($uri[0] == '/') { unset($uri[0]); } if ($uri[0] == '/') { unset($uri[0]); } } return parse_url((string) $uri); } private function gen_authority() { $t = $this; $authority = ''; if (!empty($t->user)) { $authority .= $t->user; if (empty($t->pass)) { $authority .= '@'; } else { $authority .= ':'; } } if (!empty($t->pass)) { $authority .= $t->pass.'@'; } if (!empty($t->host)) { $authority .= $t->host; } if (!empty($t->port)) { $authority .= ':'.$t->port; } $t->authority = $authority; } public function arr() { if ($this->error) { return FALSE; } return array( 'scheme' => $this->scheme, 'user' => $this->user, 'pass' => $this->pass, 'host' => $this->host, 'port' => $this->port, 'authority' => $this->authority, 'path' => $this->path, 'query' => $this->query, 'fragment' => $this->fragment ); } public function str() { if ($this->error) { return FALSE; } $t = $this; $str = ''; if (!empty($t->scheme)) { $str .= $t->scheme; } if (!empty($t->user)) { $str .= $t->user; if (empty($t->pass)) { $str .= '@'; } else { $str .= ':'; $str .= $t->pass.'@'; } } if (!empty($t->host)) { $str .= $t->host; } if (!empty($t->port)) { $str .= ':'.$t->port; } if (!empty($t->path)) { $str .= $t->path; } if (!empty($t->query)) { $str .= '?'.$t->query; } if (!empty($t->fragment)) { $str .= '#'.$t->fragment; } return $str; } public function p_str() { if ($this->error) { return FALSE; } echo $this->str(); } public function path_info() { if ($this->error) { return FALSE; } $info = pathinfo($this->path); $arr = explode('/',$this->path); $last = count($arr) - 1; if ($arr[$last] == '') { unset($arr[$last]); } if ($arr[0] == '') { array_shift($arr); } $info['array'] = $arr; return $info; } public function query_arr() { if ($this->error) { return FALSE; } parse_str($this->query, $return); return $return; } public function append($section, $str, $disable_safety = FALSE) { if ($this->error) { return FALSE; } $section = strtolower($section); if (!isset($this->$section)) { return FALSE; } if ($disable_safety) { $this->$section = $this->$section.$str; } else { $test = $this->$section.$str; $safety = $this->safety($section, $test); if ($safety != FALSE) { $this->$section = $safety; } else { return FALSE; } } $this->gen_authority(); return $this->str(); } public function prepend($section, $str, $disable_safety = FALSE) { if ($this->error) { return FALSE; } $section = strtolower($section); if (!isset($this->$section)) { return FALSE; } if ($disable_safety) { $this->$section = $str.$this->$section; } else { $test = $str.$this->$section; $safety = $this->safety($section, $test); if ($safety != FALSE) { $this->$section = $safety; } else { return FALSE; } } $this->gen_authority(); return $this->str(); } public function replace($section, $str, $disable_safety = FALSE) { if ($this->error) { return FALSE; } $section = strtolower($section); if (!isset($this->$section)) { return FALSE; } if ($disable_safety) { $this->$section = $str; } else { $safety = $this->safety($section, $str); if ($safety != FALSE) { $this->$section = $safety; } else { return FALSE; } } $this->gen_authority(); return $this->str(); } protected function safety($type, $str) { $type = strtoupper((string) $type); if ($type != 'QUERY') { $str = trim((string) $str); } $err = 0; switch ($type) { case 'SCHEME_NAME': if (!preg_match('/\A[a-z]{1,10}\Z/', $str)) { $err++; } break; case 'SCHEME': if (strpos($str, '\\') !== FALSE) { $str = str_replace('\\', '/', $str); } if (strpos($str, '//') === FALSE && stripos($str, ':') === FALSE) { if (!empty($str)) { $str = $str.'://'; // assume it is generic } else { break; // there is nothing to check } } $str = strtolower($str); if (!stripos($str, '://') === FALSE) { // explicit generic if (!preg_match('/\A[a-z]{1,10}:\/\/(\/)?\Z/', $str)) { $err++; } } elseif(stripos($str, ':') === FALSE) { // explicit pipe if (!preg_match('/\A[a-z]{1,10}:\Z/', $str)) { $err++; } } elseif(stripos($str, '//') === FALSE) { // inherit if ($str != '//') { $err++; } } break; case 'USER': $str = rawurlencode($str); break; case 'PASS': $str = rawurlencode($str); break; case 'HOST': $str = strtolower($str); if ( ( !preg_match('/\A(([a-z0-9_]([a-z0-9\-_]+)?)\.)+[a-z0-9]([a-z0-9\-]+)?\Z/', $str) // fqdn && !preg_match('/\A([0-9]\.){3}[0-9]\Z/', $str) // ip ) || strlen($str) > 255 ) { $err++; } break; case 'PORT': if ($str[0] == ':') { $str = substr($str, 1); } if (!preg_match('/\A[0-9]{0,5}\Z/', $str)) { $err++; } break; case 'PATH': $str = str_replace(array('//', '\\'), '/', $str); // common mistakes $path_arr = explode('/', $str); $safe_arr = array(); foreach ($path_arr as $path_part) { $safe_arr[] = rawurlencode($path_part); } $str = implode('/', $safe_arr); break; case 'QUERY': if (is_array($str)) { $str = http_build_query($str); } if ($str[0] == '?') { $str = substr($str, 1); } $frag_loc = strpos($str, '#'); if ($frag_loc) { $str = substr($str, 0, ($frag_loc - 1)); } elseif ($str[0] == '#') { $str = ''; } break; case 'FRAGMENT': if ($str[0] == '#') { unset($str[0]); } $str = urlencode($str); break; default: return FALSE; break; } if ($err) { return FALSE; } return $str; } public function reset() { $this->__construct($this->input); } } $uri = new uri('http://example.com/path/to/file.ext'); $uri->replace('QUERY', array('rand', '4')); $uri->replace('PATH', '/foo/bar'); $uri->append('PATH', '.baz'); $new = $uri->prepend('HOST', 'www.'); $uri->reset(); $original = $uri->str(); $uri->replace('FRAGMENT', 'Checkout'); $secure = $uri->replace('SCHEME', 'https'); echo $new.PHP_EOL; echo $original.PHP_EOL; echo $secure.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.3.60.0110.00616.73
8.3.50.0110.01022.12
8.3.40.0090.00619.20
8.3.30.0120.00319.17
8.3.20.0000.00820.46
8.3.10.0100.00323.56
8.3.00.0000.00820.96
8.2.180.0070.01116.88
8.2.170.0080.00822.96
8.2.160.0100.01020.64
8.2.150.0080.00024.18
8.2.140.0130.00624.66
8.2.130.0120.00626.16
8.2.120.0000.00820.98
8.2.110.0030.00622.36
8.2.100.0040.00818.16
8.2.90.0080.00018.13
8.2.80.0060.00319.19
8.2.70.0030.00618.00
8.2.60.0000.01017.88
8.2.50.0030.00617.88
8.2.40.0000.00818.00
8.2.30.0030.00717.97
8.2.20.0000.00918.01
8.2.10.0000.00818.20
8.2.00.0030.00617.97
8.1.280.0070.01525.92
8.1.270.0040.00423.69
8.1.260.0060.01226.35
8.1.250.0000.01828.09
8.1.240.0030.00724.02
8.1.230.0120.00419.30
8.1.220.0000.00817.91
8.1.210.0000.01018.77
8.1.200.0030.00717.60
8.1.190.0000.00817.60
8.1.180.0060.00318.10
8.1.170.0030.00517.72
8.1.160.0040.00419.02
8.1.150.0030.00518.82
8.1.140.0040.00417.74
8.1.130.0050.00218.00
8.1.120.0000.00717.80
8.1.110.0050.00317.64
8.1.100.0080.00017.70
8.1.90.0000.00817.66
8.1.80.0000.00817.65
8.1.70.0080.00017.67
8.1.60.0040.00417.76
8.1.50.0000.01017.73
8.1.40.0030.00617.82
8.1.30.0040.00417.77
8.1.20.0040.00417.84
8.1.10.0030.00517.93
8.1.00.0030.00517.73
8.0.300.0000.00818.77
8.0.290.0040.00417.13
8.0.280.0050.00318.29
8.0.270.0000.00818.40
8.0.260.0000.00717.47
8.0.250.0030.00317.32
8.0.240.0030.00617.27
8.0.230.0050.00217.19
8.0.220.0040.00417.24
8.0.210.0000.00717.14
8.0.200.0030.00317.22
8.0.190.0030.00917.26
8.0.180.0040.00417.19
8.0.170.0040.00417.33
8.0.160.0000.00817.18
8.0.150.0040.00417.27
8.0.140.0030.00617.24
8.0.130.0000.00613.68
8.0.120.0030.00517.22
8.0.110.0080.00017.24
8.0.100.0030.00617.31
8.0.90.0000.00817.24
8.0.80.0030.01317.30
8.0.70.0030.00617.15
8.0.60.0040.00417.26
8.0.50.0040.00417.27
8.0.30.0080.01317.31
8.0.20.0130.00717.43
8.0.10.0080.00017.19
8.0.00.0090.01017.08
7.4.330.0000.00515.17
7.4.320.0030.00316.80
7.4.300.0000.00716.99
7.4.290.0000.00716.88
7.4.280.0030.00616.79
7.4.270.0000.00817.01
7.4.260.0060.00316.98
7.4.250.0030.00516.89
7.4.240.0040.00416.97
7.4.230.0040.00416.78
7.4.220.0120.00616.89
7.4.210.0070.01016.93
7.4.200.0040.00417.01
7.4.160.0110.00716.88
7.4.150.0150.00417.40
7.4.140.0120.00717.86
7.4.130.0060.01316.82
7.4.120.0060.01216.90
7.4.110.0100.01317.11
7.4.100.0090.01516.98
7.4.90.0140.00416.95
7.4.80.0100.01019.39
7.4.70.0100.00716.61
7.4.60.0070.01416.77
7.4.50.0030.00616.85
7.4.40.0120.00616.88
7.4.30.0050.01116.78
7.4.10.0080.01015.41
7.4.00.0090.00715.36
7.3.330.0040.00413.64
7.3.320.0060.00013.71
7.3.310.0040.00416.55
7.3.300.0030.00316.51
7.3.290.0040.01116.67
7.3.280.0090.01216.64
7.3.270.0060.01217.40
7.3.260.0030.01516.62
7.3.250.0150.00616.67
7.3.240.0090.00916.79
7.3.230.0140.00316.67
7.3.210.0100.01316.81
7.3.200.0110.00619.39
7.3.190.0190.00316.55
7.3.180.0040.01216.59
7.3.170.0090.00816.75
7.3.160.0100.00716.77
7.3.130.0050.01215.28
7.3.120.0030.01315.09
7.3.110.0090.00415.29
7.3.100.0060.01115.31
7.3.90.0020.01315.21
7.3.80.0030.00815.22
7.3.70.0000.01415.18
7.3.60.0060.00515.08
7.3.50.0030.00715.16
7.3.40.0090.00315.05
7.3.30.0050.00715.24
7.3.20.0080.00616.84
7.3.10.0040.00816.85
7.3.00.0030.01116.91
7.2.330.0080.01117.10
7.2.320.0100.01217.13
7.2.310.0040.01417.00
7.2.300.0080.01117.09
7.2.290.0090.01216.92
7.2.260.0090.01015.46
7.2.250.0060.00915.55
7.2.240.0040.01215.47
7.2.230.0100.00615.43
7.2.220.0090.00615.38
7.2.210.0050.00815.52
7.2.200.0060.00615.43
7.2.190.0050.00915.23
7.2.180.0040.00715.29
7.2.170.0050.00915.44
7.2.160.0050.01015.54
7.2.150.0060.00617.09
7.2.140.0120.00317.17
7.2.130.0050.01117.24
7.2.120.0060.00816.99
7.2.110.0020.01116.87
7.2.100.0020.01217.18
7.2.90.0050.00817.22
7.2.80.0060.00717.31
7.2.70.0020.00917.13
7.2.60.0050.00817.26
7.2.50.0040.01017.18
7.2.40.0040.01217.14
7.2.30.0080.00417.18
7.2.20.0040.00817.14
7.2.10.0060.01017.18
7.2.00.0060.00817.66
7.1.330.0020.01115.74
7.1.320.0020.01016.07
7.1.310.0030.00616.17
7.1.300.0030.00715.82
7.1.290.0040.00815.96
7.1.280.0060.01016.10
7.1.270.0020.01216.09
7.1.260.0050.00915.88
7.1.250.0030.00915.93
7.1.240.0040.00716.10
7.1.230.0060.00615.98
7.1.220.0030.01015.91
7.1.210.0030.01115.90
7.1.200.0030.00815.95
7.1.190.0070.00716.01
7.1.180.0050.01016.07
7.1.170.0030.00915.99
7.1.160.0060.00915.94
7.1.150.0080.00516.00
7.1.140.0000.01215.93
7.1.130.0110.00416.13
7.1.120.0060.00416.12
7.1.110.0030.01016.08
7.1.100.0040.00716.86
7.1.90.0030.00815.97
7.1.80.0060.00816.09
7.1.70.0030.01016.53
7.1.60.0040.00816.62
7.1.50.0040.01116.29
7.1.40.0030.00915.96
7.1.30.0050.01016.16
7.1.20.0020.01115.92
7.1.10.0070.00716.15
7.1.00.0080.02818.11
7.0.330.0030.01015.73
7.0.320.0020.01315.50
7.0.310.0050.00515.58
7.0.300.0100.00315.49
7.0.290.0080.00515.50
7.0.280.0080.00615.53
7.0.270.0030.00715.72
7.0.260.0040.00615.60
7.0.250.0070.00815.70
7.0.240.0040.01115.76
7.0.230.0030.00715.54
7.0.220.0070.00715.67
7.0.210.0030.01115.58
7.0.200.0030.01015.97
7.0.190.0070.00815.65
7.0.180.0050.00715.69
7.0.170.0060.01115.53
7.0.160.0000.01515.41
7.0.150.0060.00715.56
7.0.140.0090.02617.77
7.0.130.0040.01015.58
7.0.120.0070.00415.68
7.0.110.0040.00915.56
7.0.100.0090.01917.55
7.0.90.0060.02017.62
7.0.80.0060.02917.65
7.0.70.0040.03217.65
7.0.60.0060.03317.73
7.0.50.0050.03117.80
7.0.40.0060.03015.76
7.0.30.0070.03115.80
7.0.20.0090.03115.84
7.0.10.0040.02615.65
7.0.00.0120.02515.89
5.6.400.0070.00814.68
5.6.390.0090.00314.45
5.6.380.0030.00814.67
5.6.370.0050.00714.51
5.6.360.0020.01314.80
5.6.350.0040.00914.77
5.6.340.0050.00814.74
5.6.330.0030.01114.61
5.6.320.0080.00614.75
5.6.310.0090.00614.87
5.6.300.0050.00714.75
5.6.290.0030.00614.70
5.6.280.0040.02916.90
5.6.270.0080.00614.73
5.6.260.0050.00614.42
5.6.250.0040.03316.60
5.6.240.0090.03016.56
5.6.230.0060.02416.54
5.6.220.0100.01816.49
5.6.210.0050.03316.60
5.6.200.0070.03216.82
5.6.190.0080.03316.79
5.6.180.0080.02916.77
5.6.170.0060.03416.69
5.6.160.0080.03516.79
5.6.150.0100.02716.94
5.6.140.0040.03216.86
5.6.130.0050.02316.88
5.6.120.0060.02816.83
5.6.110.0060.02316.88
5.6.100.0080.03016.81
5.6.90.0030.02616.97
5.6.80.0050.02116.58
5.6.70.0050.03516.55
5.6.60.0040.03316.54
5.6.50.0090.01516.47
5.6.40.0050.02116.52
5.6.30.0080.01716.41
5.6.20.0060.01816.55
5.6.10.0010.02416.43
5.6.00.0040.02016.49
5.5.380.0050.03316.54
5.5.370.0070.03216.66
5.5.360.0050.03216.53
5.5.350.0120.02816.50
5.5.340.0060.02916.63
5.5.330.0080.02916.78
5.5.320.0100.01916.60
5.5.310.0060.03316.80
5.5.300.0070.03116.75
5.5.290.0090.03116.76
5.5.280.0040.02616.74
5.5.270.0080.02916.62
5.5.260.0060.02916.73
5.5.250.0030.03516.45
5.5.240.0070.03016.44
5.5.230.0060.02416.49
5.5.220.0070.01716.33
5.5.210.0030.02016.43
5.5.200.0070.01916.45
5.5.190.0060.01816.33
5.5.180.0030.02816.49
5.5.170.0110.00514.48
5.5.160.0110.01216.47
5.5.150.0050.01716.40
5.5.140.0060.01816.44
5.5.130.0070.02016.45
5.5.120.0020.02016.50
5.5.110.0050.01716.52
5.5.100.0040.02116.41
5.5.90.0070.02216.38
5.5.80.0080.01616.41
5.5.70.0020.02216.30
5.5.60.0060.01716.48
5.5.50.0030.02016.31
5.5.40.0050.01916.46
5.5.30.0050.02516.56
5.5.20.0060.01616.38
5.5.10.0070.01916.36
5.5.00.0040.01816.34
5.4.450.0050.03314.59
5.4.440.0020.03414.49
5.4.430.0030.03114.58
5.4.420.0040.02114.50
5.4.410.0070.02814.57
5.4.400.0030.02614.42
5.4.390.0020.02914.45
5.4.380.0050.02114.47
5.4.370.0080.02114.49
5.4.360.0080.01714.43
5.4.350.0060.01914.38
5.4.340.0030.02014.48
5.4.330.0070.00712.06
5.4.320.0030.02014.38
5.4.310.0030.01914.34
5.4.300.0040.01714.45
5.4.290.0010.01914.46
5.4.280.0010.02014.43
5.4.270.0020.01914.34
5.4.260.0060.01314.42
5.4.250.0040.01914.45
5.4.240.0030.01714.49
5.4.230.0040.01814.40
5.4.220.0040.01614.42
5.4.210.0060.01514.45
5.4.200.0050.02914.47
5.4.190.0020.02014.43
5.4.180.0050.01714.41
5.4.170.0040.01714.47
5.4.160.0050.01514.40
5.4.150.0040.01614.34
5.4.140.0040.01613.53
5.4.130.0060.01413.53
5.4.120.0080.01213.53
5.4.110.0010.02113.56
5.4.100.0020.02113.56
5.4.90.0020.01613.57
5.4.80.0020.01813.55
5.4.70.0040.01613.55
5.4.60.0020.01813.54
5.4.50.0040.01613.54
5.4.40.0050.01413.59
5.4.30.0020.01813.60
5.4.20.0020.01613.57
5.4.10.0070.02813.54
5.4.00.0050.02313.27
5.3.290.0040.01812.97
5.3.280.0020.01812.91
5.3.270.0050.02112.92
5.3.260.0030.01612.92
5.3.250.0040.01612.97
5.3.240.0020.01812.95
5.3.230.0060.01312.96
5.3.220.0040.01712.91
5.3.210.0030.01912.97
5.3.200.0070.01712.96
5.3.190.0000.02112.94
5.3.180.0030.02112.91
5.3.170.0050.01512.98
5.3.160.0030.02012.96
5.3.150.0010.01812.97
5.3.140.0070.01212.97
5.3.130.0020.01812.94
5.3.120.0040.02512.93
5.3.110.0060.02012.97
5.3.100.0060.02712.75
5.3.90.0050.01912.77
5.3.80.0070.02512.76
5.3.70.0070.02712.71
5.3.60.0080.02612.71
5.3.50.0010.01912.73
5.3.40.0040.02212.70
5.3.30.0040.01912.73
5.3.20.0050.01512.69
5.3.10.0040.02912.62
5.3.00.0040.03012.59

preferences:
47.09 ms | 401 KiB | 5 Q