Location: PHPKode > scripts > URI Parameter Extraction Function > uri-parameter-extraction-function/param_extract_0.0.1.php
<?

/*	
	=========================================================================
	URI Parameter Extraction Function (param_extract) v0.01
	=========================================================================
	
	Copyright © 2002	Shaun Johnston
						hide@address.com
						Compverse / PixelCreative web design
						http://www.compverse.com.au
						Cairns, Australia
						
	For use with PHP >= 4.1.0
						
	This function is inspired by the article
		
		"Building Dynamic Pages with Search Engines in Mind" by
			Tim Perdue
			
		http://www.phpbuilder.com/columns/tim19990117.php3 
		
	This script is commented as needed. Users are encouraged to remove 
	comments for purposes of readability, but are asked to leave this header 
	intact.
	
	For sake of convenience, non-commented, and extremely compacted versions
	of the function are provided at the end of this file..
			
	=========================================================================
			
	This script is free software. You can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License.
	
	Anybody notifying this script is strongly encouraged to notify the author
	of improvements!
	
	=========================================================================
			
	Purpose:
	
	This function takes a URI and extracts the names of directories as values
	and / or associated parameters, using the forward slash ('/') as a
	delimiter.
	
	=========================================================================
	
	Reason:
	
	Various http servers use a 'guesstimate' basis for directories that don't
	exist in the virtual path. (example:
		
			http://www.xyz.com/foo/
			
			If the 'foo' directory exists, there is no guess
			needed.  However, if it doesn't, Apache will 
			default to the first file it finds (alphabetically 
			by extension) with name	matching 'foo'.  So if foo.bar 
			exists in the directory, it will be chosen.
			
			If there are 2 files, foo.bar and foo.php, foo.bar will
			be chosen because its extension is placed alphabetically
			before foo.php)
			
	Using this system, it is possible to select a script with a particular name
	by specifying the name as a directory in the URI.  It is then possible, using
	this function, to extract parameters required for this script from the URI, 
	without resorting to a querystring.  This serves two purposes:
	
		1.	Direct hyperlink masking, so that links may be presented in a
			logical hierarchy. 
			
			(eg.	http://www.xyz.com/locations/united_states/california/los_angeles/ 
					vs.
					http://www.xyz.com/locations.php?city=los_angeles&state=california&country=united_states)
		
		2.	Search Engine compatibility.  Most search engines don't index
			website pages with querystrings, but script results with a URI
			masked in this manner look like any static URI, so will be 
			indexed.
			
	=========================================================================
			
	Usage:
	
	Parameters:
	
		Name			Values		Explanation
		
		$URI_type		1			This URI uses a hierarchial structure with
									a standard sequence of directories 
									representing specific parameter values.
									Values may be separated using a delimiter
									($value_del).
									
						2			This URI uses a freeform structure, with
									Parameters in any order, and perhaps
									repeated.
									
									Parameters are separated from Values using
									a specified delimiter type ($param_del).
									
		$URI_siteroot	URI			This is the URI of the site root (eg.
									http://www.xyz.com/), or if $URI_type is 1,
									the uri of the script (eg.
									http://www.xyz.com/locations/) 
										
									For the function to work correctly, always
									place a forward slash at the end of the URI.
									
		$URI_sequence	array		If $URI_type is 1, this array is required
									to specify the sequence in which the
									parameters are structured.
									
									The array would take the following format:
									
										array[0] = $name_of_parameter1
										array[1] = $name_of_parameter2
										array[2] = $name_of_parameter3 etc...
									
		$URI			the URI		The URI that the data will be extracted
									from.
									
		$param_del		delmiter	This is the delimiter used in mode 2 URIs
									to separate parameters from values.
									
										suggestion:	'..'
										
									Using Free Form, the directory name _must_
									be in the following structure:
									
									parameter{$param_delimiter}value{$value_delimiter}value
									
									eg.
									
									city..los_angeles.las_vegas
									
		$value_del		delimeter	This is the delimiter used to separate
									values. This _must_ be different from
									$param_del.
									
										suggestion:	'.'
									
	A two-dimensional array is returned with the following structure:
	
		$array[parameter][n] = $value
		
			n being sequence number.
			
	example (free-form)
	
		URI:
	
		http://www.xyz.com/city..los_angeles.las_vegas/state..california.nevada/country..united_states/
		
		Result:
		
		array[city][0] = "los_angeles"
		array[city][1] = "las_vegas"
		array[state][0] = "california"
		array[state][1] = "nevada"
		array[country][0] = "united_states"
		
	It is recommended that in the URI preparation, spaces within value names
	either be replaced with underscores ('_') or some other non-conflicting 
	character and converted back once received, or properly URL-encoded.  
	Query Strings relating to Sessions etc should also be removed.
		
	========================================================================= 
*/

function param_extract($URI,$URI_siteroot,$URI_type,$URI_sequence,$param_del,$value_del) {
	
	//	Initialise the variable to return as an Array:
	
	$return_array = array(); 

	//	Trim the URI Root:

	$URI = str_replace($URI_siteroot, "", $URI);
	
	//	Extract Each Component from the URI:
	
	$URI_components = explode("/", $URI);
	
	//	Get the number of Components:
	
	$num_of_components = sizeof($URI_components);
	
	//	Determine the URI Type:
	
	switch($URI_type) {
		
		//	If the URI Type is Hierarchial (mode 1):
		
		case 1:
			
			//	Extraction Procedure for Hierarchial URI:
			
			$size_of_sequence = sizeof($URI_sequence);
			
			//	Determine How Many Iterations to go through to get values:

			if ($size_of_sequence > $num_of_components) {
				$max = $num_of_components;
				}
			else {
				$max = $size_of_sequence;
				}

			//	Begin extraction sequence:
				
			for ($i = 0; $i < $max; $i++) {
			
				//	Assign current parameter to an array key: 
				
				$param_name = $URI_sequence[$i];
				
				//	Get Values from this component:
				
				$this_component = explode($value_del, $URI_components[$i]);
				
				$size_of_this = sizeof($this_component);
				
				//	Use a sequence to place extracted values into an array:
				
				for ($ii = 0; $ii < $size_of_this; $ii++) {
				
					//	Assign Current Value to this Array Instance:
					
					$return_array[$param_name][$ii] = $this_component[$ii];
										
					}	//	loop $ii
					
				}	//	loop $i
			break;	//	case 1
		
		//	If the URI Type is Free Form (mode 2):
		
		case 2:
		
			//	Extraction Procedure for Free-Form URI:
			
			//	Determine Number of Iterations to go through:

			$max = $num_of_components;

			//	Begin extraction sequence:
				
			for ($i = 0; $i < $max; $i++) {
			
				//	Separate Parameter from Values:
				
				$this_component = explode($param_del, $URI_components[$i]);

				//	Assign current parameter to an array key:
				
				$param_name = $this_component[0];
				
				//	Assign the values to an array:
				
				$this_values = explode($value_del, $this_component[1]);
				
				// 	If a duplicate of the current parameter is found, continue
				//	assignment sequence for that array:
				
				if (isset($return_array[$param_name])) {
					
					//	Get the size of the duplicate array as a sequence base:
					
					$min = sizeof($return_array[$param_name]);
					
					//	Determine sequence max by adding duplicate array size
					//	to this value array's size:
					
					$max = $min + sizeof($this_values);
					
					}
					
				//	If no duplicate is found, $min = 0, $max = size of
				//	this value array:					

				else {
					
					$imin = 0;
					$imax = sizeof($this_values);
					
					}
					
				//	Begin Assignment Sequence:
				
				for ($ii = $imin; $ii < $imax; $ii++) {
					
					//	Assign Current Value to this Array Instance

					$return_array[$param_name][$ii] = $this_values[$ii];
					
					}	//	loop $ii
					
				}	//	loop $i
				
			break;	//	case 2
			
		}	// end switch
		
	return $return_array;
	
	}	//	end param_extract

	
/*
	========================================================================= 
	Non-Commented Version
	=========================================================================	
*/

/*
	=========================================================================
	URI Parameter Extraction Function (param_extract) v0.01
	=========================================================================
	
	Copyright © 2002	Shaun Johnston
						hide@address.com
						Compverse / PixelCreative web design
						http://www.compverse.com.au
						Cairns, Australia
						
	For use with PHP >= 4.1.0
						
	This function is inspired by the article
		
		"Building Dynamic Pages with Search Engines in Mind" by
			Tim Perdue
			
		http://www.phpbuilder.com/columns/tim19990117.php3 
			
	=========================================================================
			
	This script is free software. You can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License.
	
	Anybody modifying this script is strongly encouraged to notify the 
	author of improvements!
	
	=========================================================================

function param_extract($URI,$URI_siteroot,$URI_type,$URI_sequence,$param_del,$value_del) {
	$return_array = array(); 
	$URI = str_replace($URI_siteroot, "", $URI);
	$URI_components = explode("/", $URI);
	$num_of_components = sizeof($URI_components);
	switch($URI_type) {
		case 1:
			$size_of_sequence = sizeof($URI_sequence);
			if ($size_of_sequence > $num_of_components) {
				$max = $num_of_components;
				}
			else {
				$max = $size_of_sequence;
				}	
			for ($i = 0; $i < $max; $i++) {
				$param_name = $URI_sequence[$i];
				$this_component = explode($value_del, $URI_components[$i]);
				$size_of_this = sizeof($this_component);
				for ($ii = 0; $ii < $size_of_this; $ii++) {
					$return_array[$param_name][$ii] = $this_component[$ii];					
					}
				}
			break;
		case 2:
			$max = $num_of_components;
			for ($i = 0; $i < $max; $i++) {
				$this_component = explode($param_del, $URI_components[$i]);
				$param_name = $this_component[0];
				$this_values = explode($value_del, $this_component[1]);
				if (isset($return_array[$param_name])) {
					$min = sizeof($return_array[$param_name]);
					$max = $min + sizeof($this_values);
					}
				else {
					$imin = 0;
					$imax = sizeof($this_values);
					}
				for ($ii = $imin; $ii < $imax; $ii++) {
					$return_array[$param_name][$ii] = $this_values[$ii];
					}
				}
			break;
		}
	return $return_array;
	}
*/
/*
	========================================================================= 
	Compacted Version
	=========================================================================	
*/
/*
function param_extract($f,$b,$c,$d,$g,$h){$p=array();$f=str_replace($b,"",$f);
$e=explode("/",$f);$h=sizeof($e);switch($c){case 1:$i=sizeof($d);if($i>$h){$j=
$h;}else{$j=$i;}for($i=0;$i<$j;$i++){$k=$d[$i];$l=explode($h,$e[$i]);$m=sizeof
($l);for($n=0;$n<$m;$n++){$p[$k][$n]=$l[$n];}}break;case 2:$j=$h;for($i=0;$i<
$j;$i++){$l=explode($g,$e[$i]);$k=$l[0];$o=explode($h,$l[1]);if(isset($p[$k]))
{$r=sizeof($p[$k]);$j=$r+sizeof($o);}else{$s=0;$q=sizeof($o);}for($n=$s;$n<$q;
$n++){$p[$k][$n]=$o[$n];}}break;}return($p);}
*/

?>
Return current item: URI Parameter Extraction Function