Return to Snippet

Revision: 6592
at January 29, 2009 01:08 by dbug13


Updated Code
<?php
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, port, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [port] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com:8080/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [port] => 8080
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
			
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['port'] = $matches[4];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[6];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}
?>

Revision: 6591
at January 28, 2009 15:48 by dbug13


Updated Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, port, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [port] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com:8080/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [port] => 8080
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
			
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['port'] = $matches[4];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[6];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Revision: 6590
at May 31, 2008 21:15 by dbug13


Updated Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 /**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, port, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [port] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com:8080/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [port] => 8080
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
			
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['port'] = $matches[4];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[6];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Revision: 6589
at May 31, 2008 21:10 by dbug13


Updated Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 /**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [port] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com:8080/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [port] => 8080
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
			
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['port'] = $matches[4];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[6];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Revision: 6588
at May 31, 2008 21:02 by dbug13


Updated Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
		
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[4];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Revision: 6587
at May 31, 2008 21:01 by dbug13


Updated Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*([\w\.\d]*)(/*)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
	
	print_r($matches);
	
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[4];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Revision: 6586
at May 31, 2008 20:47 by dbug13


Initial Code
/**
 * Takes an URL and splits it up into it's protocol, site, and resource parts
 * 
 * Returns an associative array of protocol, site, and resource parts
 * Note: the URL does not have to have a protocol specified 
 * example:
 * <code>
 *     $url = "http://www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] => http
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 *     
 *     $url = "www.foo.com/blog/search?q=bar";
 *     $url_parts = getUrlParts($url);
 *     // $url_parts will contain the following:
 *     // Array
 *     // (
 *     //     [protocol] =>
 *     //     [site] => www.foo.com
 *     //     [resource] => blog/search?q=bar
 *     // )
 * </code>
 * 
 * @param string	The URL that you want to split up
 * @return associative array 	Array containing the split up parts of the URL
 */
function getUrlParts($url) {
	$result = array();
	
	// Get the protocol, site and resource parts of the URL
	// original url = http://example.com/blog/index?name=foo
	// protocol = http://
	// site = example.com/
	// resource = blog/index?name=foo
	$regex = '#^(.*?//)*(.*?/)(.*)$#';
	$matches = array();
	preg_match($regex, $url, $matches);
	
	// Assign the matched parts of url to the result array
	$result['protocol'] = $matches[1];
	$result['site'] = $matches[2];
	$result['resource'] = $matches[3];
	
	// clean up the site portion by removing the trailing /
	$result['site'] = preg_replace('#/$#', '', $result['site']);
	
	// clean up the protocol portion by removing the trailing ://
	$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
	
	return $result;
}

Initial URL


Initial Description

 

Initial Title
Split an URL into protocol, site, and resource parts

Initial Tags
php, textmate

Initial Language
PHP