Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Created January 9, 2016 10:46
Show Gist options
  • Save bryanwillis/7976c00d5f94ed0c2985 to your computer and use it in GitHub Desktop.
Save bryanwillis/7976c00d5f94ed0c2985 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";
}
@saber13812002
Copy link

how to have separate port from url in php ?
how can dispatch the domain name username passwords in the url
how can parse the url and get query string in it
this is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment