<?php
namespace NotGlobal;
const PHP_URL_SCHEME = 0x001;
const PHP_URL_USER = 0x002;
const PHP_URL_PASS = 0x004;
const PHP_URL_HOST = 0x008;
const PHP_URL_PORT = 0x010;
const PHP_URL_PATH = 0x020;
const PHP_URL_QUERY = 0x040;
const PHP_URL_FRAGMENT = 0x080;
const PHP_URL_REQUIRE_SCHEME = 0x100;
const PHP_URL_DISALLOW_EMPTY_AUTHORITY = 0x200;
function parse_url($url, $flags = 0)
{
static $map = [
PHP_URL_SCHEME => 'scheme',
PHP_URL_USER => 'user',
PHP_URL_PASS => 'pass',
PHP_URL_HOST => 'host',
PHP_URL_PORT => 'port',
PHP_URL_PATH => 'path',
PHP_URL_QUERY => 'query',
PHP_URL_FRAGMENT => 'fragment',
];
static $pattern = <<<REGEXP
!
^
(?: (?P<scheme> [a-z][a-z0-9+.\-]* ) : )?
(?:
(?<hasauthority> // )
(?:
(?P<user> [^:@/\s]+ )
(?: : (?P<pass> [^:@/\s]+ ) )?
@
)?
(?P<host> [^:/?#\s]* )
(?: : (?P<port> [0-9]+ ) )?
(?= [/?#]|$ )
)?
(?P<path> [^?#\s]+ )?
(?: ? (?P<query> [^#\s]+ ) )?
(?: # (?P<fragment> \S+ ) )?
$
!xi
REGEXP;
if (!preg_match($pattern, trim($url), $matches)) {
return false;
}
if ($flags & PHP_URL_REQUIRE_SCHEME) {
if (!isset($matches['scheme']) || $matches['scheme'] === '') {
return false;
}
}
if ($flags & PHP_URL_DISALLOW_EMPTY_AUTHORITY) {
if (isset($matches['hasauthority']) && $matches['hasauthority'] && (!isset($matches['host']) || $matches['host'] === '')) {
return false;
}
}
// Default to all components, allowing the above flags
// to be specified without specifying the components
$flags = $flags & 0xFF ?: 0xFF;
$result = [];
foreach ($map as $flag => $component) {
if (($flags & $flag) && isset($matches[$component]) && $matches[$component] !== '') {
$result[$component] = $flag === PHP_URL_PORT ? (int) $matches[$component] : $matches[$component];
}
}
return count($result) === 1 ? end($result) : $result;
}
$url = 'http://user:pass@foo.com:123456/test?thing=stuff#fragment';
var_dump(parse_url($url, -1));
- Output for git.master, git.master_jit, rfc.property-hooks
- Warning: preg_match(): Compilation failed: quantifier does not follow a repeatable item at offset 309 in /in/LBcMX on line 51
bool(false)
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
47.28 ms | 401 KiB | 8 Q