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', (string) rand(1, 10))); $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)
7.3.120.0130.00315.20
7.3.110.0040.00815.40
7.3.100.0100.01015.00
7.3.90.0030.00615.04
7.3.80.0090.00615.12
7.3.70.0100.00715.13
7.3.60.0030.01315.25
7.3.50.0060.00915.08
7.3.40.0060.00915.24
7.3.30.0080.00815.18
7.3.20.0000.01917.04
7.3.10.0080.00816.73
7.3.00.0070.01116.97
7.2.240.0030.01015.39
7.2.230.0060.01015.26
7.2.220.0000.01115.28
7.2.210.0130.00615.43
7.2.200.0090.00915.29
7.2.190.0130.00315.48
7.2.180.0100.00015.58
7.2.170.0060.00615.48
7.2.160.0100.00015.54
7.2.150.0000.01417.31
7.2.140.0110.00717.11
7.2.130.0100.01017.23
7.2.120.0040.00817.08
7.2.110.0060.01017.15
7.2.100.0100.00717.16
7.2.90.0000.01217.38
7.2.80.0090.00917.33
7.2.70.0100.00717.18
7.2.60.0090.00917.08
7.2.50.0070.01117.21
7.2.40.0070.01117.23
7.2.30.0100.01017.10
7.2.20.0060.00917.28
7.2.10.0060.01616.91
7.2.00.0030.00917.07
7.1.330.0110.00316.20
7.1.320.0070.01015.94
7.1.310.0070.00415.89
7.1.300.0030.00915.98
7.1.290.0040.01115.87
7.1.280.0000.01715.89
7.1.270.0120.00316.14
7.1.260.0030.00915.97
7.1.250.0040.01116.00
7.1.70.0040.00717.59
7.1.60.0040.00717.17
7.1.50.0090.01516.89
7.1.00.0030.07722.48
7.0.200.0070.01016.88
7.0.140.0030.07322.07
7.0.100.0070.04020.20
7.0.90.0100.04320.10
7.0.80.0100.04020.14
7.0.70.0170.03720.08
7.0.60.0100.04020.02
7.0.50.0070.07020.54
7.0.40.0000.09020.19
7.0.30.0100.06020.11
7.0.20.0170.07720.03
7.0.10.0170.06320.03
7.0.00.0100.08320.03
5.6.280.0100.07021.09
5.6.250.0000.04720.60
5.6.240.0030.07020.58
5.6.230.0100.03720.76
5.6.220.0000.05020.65
5.6.210.0000.04320.56
5.6.200.0070.07021.02
5.6.190.0030.09021.11
5.6.180.0130.07321.01
5.6.170.0170.07021.08
5.6.160.0100.08021.04
5.6.150.0200.06321.07
5.6.140.0130.07321.12
5.6.130.0170.08321.01
5.6.120.0070.08321.11
5.6.110.0000.08721.23
5.6.100.0070.07721.16
5.6.90.0100.07321.01
5.6.80.0100.07720.53
5.6.70.0070.08720.50
5.6.60.0030.08320.32
5.6.50.0070.08320.40
5.6.40.0130.07020.60
5.6.30.0100.07720.46
5.6.20.0030.07320.56
5.6.10.0200.06320.55
5.6.00.0100.07720.48
5.5.380.0070.05320.45
5.5.370.0100.03320.43
5.5.360.0070.03720.51
5.5.350.0000.04320.50
5.5.340.0070.05720.93
5.5.330.0100.08020.91
5.5.320.0030.08720.97
5.5.310.0170.07721.00
5.5.300.0070.08320.90
5.5.290.0100.04720.88
5.5.280.0070.07720.93
5.5.270.0030.08320.95
5.5.260.0130.07720.82
5.5.250.0100.08020.77
5.5.240.0030.05020.19
5.5.230.0070.08320.32
5.5.220.0130.06320.31
5.5.210.0070.07320.34
5.5.200.0130.06720.33
5.5.190.0130.07020.30
5.5.180.0070.07720.29
5.5.160.0130.05320.24
5.5.150.0170.07020.30
5.5.140.0070.08320.30
5.5.130.0000.08720.25
5.5.120.0100.07720.26
5.5.110.0170.07020.29
5.5.100.0070.08020.19
5.5.90.0130.07720.15
5.5.80.0100.07320.22
5.5.70.0070.04320.23
5.5.60.0070.07720.13
5.5.50.0100.07020.13
5.5.40.0130.06720.16
5.5.30.0070.07320.07
5.5.20.0000.08320.16
5.5.10.0230.06020.16
5.5.00.0030.08320.02
5.4.450.0030.08719.36
5.4.440.0030.08719.23
5.4.430.0130.07719.45
5.4.420.0100.07719.38
5.4.410.0100.08019.15
5.4.400.0000.08719.06
5.4.390.0070.07718.89
5.4.380.0070.08018.96
5.4.370.0100.07019.24
5.4.360.0100.08019.14
5.4.350.0030.08319.06
5.4.340.0070.08018.84
5.4.320.0100.08019.06
5.4.310.0070.06319.03
5.4.300.0100.07319.18
5.4.290.0070.04319.04
5.4.280.0100.07318.90
5.4.270.0070.07719.14
5.4.260.0100.06719.04
5.4.250.0170.07019.13
5.4.240.0100.07319.14
5.4.230.0170.06719.24
5.4.220.0100.07719.12
5.4.210.0000.08318.92
5.4.200.0170.06319.21
5.4.190.0130.07019.04
5.4.180.0200.07018.92
5.4.170.0130.07319.13
5.4.160.0170.06719.02
5.4.150.0130.07019.12
5.4.140.0030.07716.20
5.4.130.0030.05716.43
5.4.120.0070.07316.21
5.4.110.0100.07316.50
5.4.100.0070.07016.48
5.4.90.0100.06716.47
5.4.80.0100.07016.46
5.4.70.0030.07716.48
5.4.60.0100.06716.43
5.4.50.0130.07016.42
5.4.40.0030.07316.38
5.4.30.0100.07316.32
5.4.20.0130.07016.45
5.4.10.0030.06716.52
5.4.00.0170.06015.85
5.3.290.0070.07714.84
5.3.280.0130.06714.68
5.3.270.0070.08314.74
5.3.260.0100.07314.85
5.3.250.0070.07714.84
5.3.240.0170.05714.72
5.3.230.0170.05714.77
5.3.220.0070.07014.74
5.3.210.0000.07014.61
5.3.200.0030.04714.68
5.3.190.0070.07314.80
5.3.180.0100.07014.74
5.3.170.0100.07014.65
5.3.160.0030.04714.74
5.3.150.0100.07714.69
5.3.140.0070.07014.55
5.3.130.0070.05314.78
5.3.120.0070.07714.67
5.3.110.0030.08014.75
5.3.100.0100.07714.21
5.3.90.0100.08014.10
5.3.80.0100.06714.15
5.3.70.0070.07714.11
5.3.60.0070.07014.25
5.3.50.0030.08714.05
5.3.40.0100.07314.13
5.3.30.0070.07314.08
5.3.20.0030.05013.82
5.3.10.0070.06713.90
5.3.00.0130.06713.80

preferences:
34.32 ms | 400 KiB | 5 Q