@ 2014-07-10T00:20:31Z <?php
<?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) 5.4.30 0.007 0.049 12.50 5.4.29 0.005 0.038 12.49 5.4.28 0.008 0.033 12.39 5.4.27 0.007 0.040 12.39 5.4.26 0.006 0.038 12.39 5.4.25 0.005 0.049 12.39 5.4.24 0.005 0.041 12.39 5.4.23 0.006 0.038 12.38 5.4.22 0.006 0.036 12.38 5.4.21 0.007 0.035 12.38 5.4.20 0.006 0.039 12.38 5.4.19 0.005 0.037 12.38 5.4.18 0.005 0.036 12.38 5.4.17 0.006 0.038 12.38 5.4.16 0.010 0.044 12.38 5.4.15 0.010 0.038 12.38 5.4.14 0.009 0.036 12.07 5.4.13 0.010 0.039 12.05 5.4.12 0.007 0.036 12.01 5.4.11 0.008 0.034 12.01 5.4.10 0.010 0.031 12.01 5.4.9 0.008 0.035 12.01 5.4.8 0.004 0.039 12.02 5.4.7 0.007 0.036 12.00 5.4.6 0.004 0.039 12.00 5.4.5 0.006 0.036 12.00 5.4.4 0.005 0.036 11.99 5.4.3 0.006 0.037 12.00 5.4.2 0.004 0.037 11.99 5.4.1 0.010 0.031 11.99 5.4.0 0.004 0.037 11.49 5.3.28 0.009 0.043 12.70 5.3.27 0.006 0.043 12.72 5.3.26 0.007 0.042 12.72 5.3.25 0.011 0.043 12.72 5.3.24 0.007 0.039 12.72 5.3.23 0.008 0.038 12.71 5.3.22 0.007 0.036 12.68 5.3.21 0.005 0.040 12.68 5.3.20 0.005 0.037 12.68 5.3.19 0.004 0.039 12.68 5.3.18 0.007 0.035 12.67 5.3.17 0.005 0.038 12.67 5.3.16 0.007 0.035 12.67 5.3.15 0.005 0.039 12.67 5.3.14 0.007 0.044 12.66 5.3.13 0.006 0.043 12.65 5.3.12 0.005 0.041 12.66 5.3.11 0.007 0.037 12.66 5.3.10 0.006 0.037 12.12 5.3.9 0.006 0.036 12.08 5.3.8 0.006 0.041 12.07 5.3.7 0.008 0.034 12.07 5.3.6 0.004 0.039 12.07 5.3.5 0.004 0.036 12.00 5.3.4 0.005 0.041 12.00 5.3.3 0.005 0.035 11.95 5.3.2 0.006 0.036 11.73 5.3.1 0.007 0.035 11.70 5.3.0 0.007 0.038 11.68
preferences:dark mode live preview
139.59 ms | 1394 KiB | 7 Q