URL_Parse() - Parse out the parts of a standard URL

PHP Code

	function URL_Parse($url) {
		/*
		Purpose
			Used to parse URL in a very predictable way.
		Parameters
			url (string) = The URL we want to parse.
		Return (associative array)
			With this input:
				"foo://username:password@[2001:4860:0:2001::68]:8042/over/there/index.dtb;type=animal?name=ferret#nose"
			 This function will return:
				[scheme] => foo
				[userinfo] => username:password
				[username] => username
				[password] => password
				[authority] => username:password@[2001:4860:0:2001::68]:8042
				[host] => [2001:4860:0:2001::68]
				[port] => 8042
				[path] => /over/there/index.dtb;type=animal
				[query] => name=ferret
				[fragment] => nose
			If any of these fields are missing in the original URL, the corresponding array element is zero-length.
		Version History
			1.0 2009.11.01 Tested on PHP 5.1.6 by Russ Tanner
				Code heavily borrowed from fredludd (at) gmail.com.
				Modified by theoriginalmarksimpson (at) gmail.com
				Modified by russtanner (at) gmail.com
		*/
		
		$r  = "(?:([a-z0-9+-._]+)://)?";
		$r .= "(?:";
		$r .= "(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?";
		$r .= "(?:\[((?:[a-z0-9:])*)\])?";
		$r .= "((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9a-f]{2})*)";
		$r .= "(?::(\d*))?";
		$r .= "(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9a-f]{2})*)?";
		$r .= "|";
		$r .= "(/?";
		$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+";
		$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9a-f]{2})*";
		$r .= ")?";
		$r .= ")";
		$r .= "(?:\?((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
		$r .= "(?:#((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
		
		preg_match("`$r`i", $url, $match);

		$parts = array(
			'scheme'=>'',
			'userinfo'=>'',
			'authority'=>'',
			'host'=> '',
			'port'=>'',
			'path'=>'',
			'query'=>'',
			'fragment'=>'');

		switch (count ($match)) {
			case 10: $parts['fragment'] = $match[9];
			case 9: $parts['query'] = $match[8];
			case 8: $parts['path'] = $match[7];
			case 7: $parts['path'] = $match[6] . $parts['path'];
			case 6: $parts['port'] = $match[5];
			case 5: $parts['host'] = $match[3] ? '[' . $match[3] . ']' : $match[4];
			case 4: $parts['userinfo'] = $match[2];
			case 3: $parts['scheme'] = $match[1];
		}

		$parts['authority'] = ($parts['userinfo'] ? $parts['userinfo'] . '@' : '') . $parts['host'] . ($parts['port'] ? ':' . $parts
['port'] : '');

		list($parts['username'], $parts['password']) = explode(':', $parts['userinfo'] . ':');

		return $parts;

	}
 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)