Skip to content

Instantly share code, notes, and snippets.

@saber13812002
Forked from bryanwillis/parse-url.php
Created June 14, 2018 09:25
Show Gist options
  • Save saber13812002/5c018925254d48ebdbc1be406ba1c91a to your computer and use it in GitHub Desktop.
Save saber13812002/5c018925254d48ebdbc1be406ba1c91a to your computer and use it in GitHub Desktop.
Parse url. Break down a url to get the parameters / arguments. Very helpful....
<?php
$url = 'http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment';
if ($url === unparse_url(parse_url($url))) {
print "YES, they match!\n";
}
function unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment