@ 2014-07-10T00:21:02Z <?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;
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 7.3.12 0.013 0.003 15.20 7.3.11 0.004 0.008 15.40 7.3.10 0.010 0.010 15.00 7.3.9 0.003 0.006 15.04 7.3.8 0.009 0.006 15.12 7.3.7 0.010 0.007 15.13 7.3.6 0.003 0.013 15.25 7.3.5 0.006 0.009 15.08 7.3.4 0.006 0.009 15.24 7.3.3 0.008 0.008 15.18 7.3.2 0.000 0.019 17.04 7.3.1 0.008 0.008 16.73 7.3.0 0.007 0.011 16.97 7.2.24 0.003 0.010 15.39 7.2.23 0.006 0.010 15.26 7.2.22 0.000 0.011 15.28 7.2.21 0.013 0.006 15.43 7.2.20 0.009 0.009 15.29 7.2.19 0.013 0.003 15.48 7.2.18 0.010 0.000 15.58 7.2.17 0.006 0.006 15.48 7.2.16 0.010 0.000 15.54 7.2.15 0.000 0.014 17.31 7.2.14 0.011 0.007 17.11 7.2.13 0.010 0.010 17.23 7.2.12 0.004 0.008 17.08 7.2.11 0.006 0.010 17.15 7.2.10 0.010 0.007 17.16 7.2.9 0.000 0.012 17.38 7.2.8 0.009 0.009 17.33 7.2.7 0.010 0.007 17.18 7.2.6 0.009 0.009 17.08 7.2.5 0.007 0.011 17.21 7.2.4 0.007 0.011 17.23 7.2.3 0.010 0.010 17.10 7.2.2 0.006 0.009 17.28 7.2.1 0.006 0.016 16.91 7.2.0 0.003 0.009 17.07 7.1.33 0.011 0.003 16.20 7.1.32 0.007 0.010 15.94 7.1.31 0.007 0.004 15.89 7.1.30 0.003 0.009 15.98 7.1.29 0.004 0.011 15.87 7.1.28 0.000 0.017 15.89 7.1.27 0.012 0.003 16.14 7.1.26 0.003 0.009 15.97 7.1.25 0.004 0.011 16.00 7.1.7 0.004 0.007 17.59 7.1.6 0.004 0.007 17.17 7.1.5 0.009 0.015 16.89 7.1.0 0.003 0.077 22.48 7.0.20 0.007 0.010 16.88 7.0.14 0.003 0.073 22.07 7.0.10 0.007 0.040 20.20 7.0.9 0.010 0.043 20.10 7.0.8 0.010 0.040 20.14 7.0.7 0.017 0.037 20.08 7.0.6 0.010 0.040 20.02 7.0.5 0.007 0.070 20.54 7.0.4 0.000 0.090 20.19 7.0.3 0.010 0.060 20.11 7.0.2 0.017 0.077 20.03 7.0.1 0.017 0.063 20.03 7.0.0 0.010 0.083 20.03 5.6.28 0.010 0.070 21.09 5.6.25 0.000 0.047 20.60 5.6.24 0.003 0.070 20.58 5.6.23 0.010 0.037 20.76 5.6.22 0.000 0.050 20.65 5.6.21 0.000 0.043 20.56 5.6.20 0.007 0.070 21.02 5.6.19 0.003 0.090 21.11 5.6.18 0.013 0.073 21.01 5.6.17 0.017 0.070 21.08 5.6.16 0.010 0.080 21.04 5.6.15 0.020 0.063 21.07 5.6.14 0.013 0.073 21.12 5.6.13 0.017 0.083 21.01 5.6.12 0.007 0.083 21.11 5.6.11 0.000 0.087 21.23 5.6.10 0.007 0.077 21.16 5.6.9 0.010 0.073 21.01 5.6.8 0.010 0.077 20.53 5.6.7 0.007 0.087 20.50 5.6.6 0.003 0.083 20.32 5.6.5 0.007 0.083 20.40 5.6.4 0.013 0.070 20.60 5.6.3 0.010 0.077 20.46 5.6.2 0.003 0.073 20.56 5.6.1 0.020 0.063 20.55 5.6.0 0.010 0.077 20.48 5.5.38 0.007 0.053 20.45 5.5.37 0.010 0.033 20.43 5.5.36 0.007 0.037 20.51 5.5.35 0.000 0.043 20.50 5.5.34 0.007 0.057 20.93 5.5.33 0.010 0.080 20.91 5.5.32 0.003 0.087 20.97 5.5.31 0.017 0.077 21.00 5.5.30 0.007 0.083 20.90 5.5.29 0.010 0.047 20.88 5.5.28 0.007 0.077 20.93 5.5.27 0.003 0.083 20.95 5.5.26 0.013 0.077 20.82 5.5.25 0.010 0.080 20.77 5.5.24 0.003 0.050 20.19 5.5.23 0.007 0.083 20.32 5.5.22 0.013 0.063 20.31 5.5.21 0.007 0.073 20.34 5.5.20 0.013 0.067 20.33 5.5.19 0.013 0.070 20.30 5.5.18 0.007 0.077 20.29 5.5.16 0.013 0.053 20.24 5.5.15 0.017 0.070 20.30 5.5.14 0.007 0.083 20.30 5.5.13 0.000 0.087 20.25 5.5.12 0.010 0.077 20.26 5.5.11 0.017 0.070 20.29 5.5.10 0.007 0.080 20.19 5.5.9 0.013 0.077 20.15 5.5.8 0.010 0.073 20.22 5.5.7 0.007 0.043 20.23 5.5.6 0.007 0.077 20.13 5.5.5 0.010 0.070 20.13 5.5.4 0.013 0.067 20.16 5.5.3 0.007 0.073 20.07 5.5.2 0.000 0.083 20.16 5.5.1 0.023 0.060 20.16 5.5.0 0.003 0.083 20.02 5.4.45 0.003 0.087 19.36 5.4.44 0.003 0.087 19.23 5.4.43 0.013 0.077 19.45 5.4.42 0.010 0.077 19.38 5.4.41 0.010 0.080 19.15 5.4.40 0.000 0.087 19.06 5.4.39 0.007 0.077 18.89 5.4.38 0.007 0.080 18.96 5.4.37 0.010 0.070 19.24 5.4.36 0.010 0.080 19.14 5.4.35 0.003 0.083 19.06 5.4.34 0.007 0.080 18.84 5.4.32 0.010 0.080 19.06 5.4.31 0.007 0.063 19.03 5.4.30 0.010 0.073 19.18 5.4.29 0.007 0.043 19.04 5.4.28 0.010 0.073 18.90 5.4.27 0.007 0.077 19.14 5.4.26 0.010 0.067 19.04 5.4.25 0.017 0.070 19.13 5.4.24 0.010 0.073 19.14 5.4.23 0.017 0.067 19.24 5.4.22 0.010 0.077 19.12 5.4.21 0.000 0.083 18.92 5.4.20 0.017 0.063 19.21 5.4.19 0.013 0.070 19.04 5.4.18 0.020 0.070 18.92 5.4.17 0.013 0.073 19.13 5.4.16 0.017 0.067 19.02 5.4.15 0.013 0.070 19.12 5.4.14 0.003 0.077 16.20 5.4.13 0.003 0.057 16.43 5.4.12 0.007 0.073 16.21 5.4.11 0.010 0.073 16.50 5.4.10 0.007 0.070 16.48 5.4.9 0.010 0.067 16.47 5.4.8 0.010 0.070 16.46 5.4.7 0.003 0.077 16.48 5.4.6 0.010 0.067 16.43 5.4.5 0.013 0.070 16.42 5.4.4 0.003 0.073 16.38 5.4.3 0.010 0.073 16.32 5.4.2 0.013 0.070 16.45 5.4.1 0.003 0.067 16.52 5.4.0 0.017 0.060 15.85 5.3.29 0.007 0.077 14.84 5.3.28 0.013 0.067 14.68 5.3.27 0.007 0.083 14.74 5.3.26 0.010 0.073 14.85 5.3.25 0.007 0.077 14.84 5.3.24 0.017 0.057 14.72 5.3.23 0.017 0.057 14.77 5.3.22 0.007 0.070 14.74 5.3.21 0.000 0.070 14.61 5.3.20 0.003 0.047 14.68 5.3.19 0.007 0.073 14.80 5.3.18 0.010 0.070 14.74 5.3.17 0.010 0.070 14.65 5.3.16 0.003 0.047 14.74 5.3.15 0.010 0.077 14.69 5.3.14 0.007 0.070 14.55 5.3.13 0.007 0.053 14.78 5.3.12 0.007 0.077 14.67 5.3.11 0.003 0.080 14.75 5.3.10 0.010 0.077 14.21 5.3.9 0.010 0.080 14.10 5.3.8 0.010 0.067 14.15 5.3.7 0.007 0.077 14.11 5.3.6 0.007 0.070 14.25 5.3.5 0.003 0.087 14.05 5.3.4 0.010 0.073 14.13 5.3.3 0.007 0.073 14.08 5.3.2 0.003 0.050 13.82 5.3.1 0.007 0.067 13.90 5.3.0 0.013 0.067 13.80
preferences:dark mode live preview
34.32 ms | 400 KiB | 5 Q