Return to Snippet

Revision: 45267
at April 28, 2011 01:22 by tasmanweb


Updated Code
<?php
/* 
Extended parse_url
 parses a url correctly even with a url as query parameter
 also retuns dirname, basename and extension besides the other parse_url bits
 if $parsequery is set to TRUE the query variables and their values are returned as part of the array, this also applies to  mailto links
 also correctly parses mailto links with the email showing up as the path 
 note to self: remember extension does not include the point
 if $retempty is set to true empty values will be returned avoiding "nonexistand index" errors, if false then empty values will be erased
 Also returns isdirectory indicating whether the url points to a file or directory
 */
function parseurl($url,$retempty=TRUE,$parsequery=FALSE) {
	// $url_default determines the order of the  array elements adjust to your needs, any key not mentioned in it gets stuck on the end, rearrange to suit your needs
	// this empty array is also used in line 36 in order to avoid "nonexistant index" errors.
	$url_default = array( 
	"original" => "",
	"scheme" => "", 
	"host" => "", 
	"port" => "", 
	"user" => "", 
	"pass" => "",
	"isdirectory" => "",
	"path" => "",
	"dirname" => "",
	"basename" => "",
	"extension" => "",
	"fragment" => "",
	"query" => "" 
	);
	$qpos = strpos($url, '?'); 
	if (!empty($qpos)) {	
		$baseurl = substr($url, 0, $qpos);		
		$bits = @parse_url($baseurl);

		$bits['basename'] = @basename($bits['path']);	
		$bits['query'] = substr($url, $qpos+1);
	} else { 
		$bits = @parse_url($url);		
	}
	$bits = array_merge($url_default,$bits); // avoid non existend index errors 
	if (strtolower($bits['scheme']) == 'mailto') {
		unset($bits['basename']); // no need for basename as it duplicates the path value, just confuses things
	} else {
		$bits = array_merge($bits,pathinfo($bits['path'])); // no need to do this for mailto's
	}
	if (!empty($bits['path'])){
		if ($bits['path'][strlen($bits['path']) - 1] == '/'){
			$bits['isdirectory'] = "TRUE";
		} else { $bits['isdirectory'] = "FALSE";}
	}
	if ($bits['dirname'] == '\\') $bits['dirname'] = '/';
	$bits['original']=$url; // add the original url to the array, comment this line to leave it out
	if ($parsequery === TRUE) $bits = array_merge($bits,parsequery($bits['query']));
	
	$bits = array_merge($url_default,$bits); //set the order of elements
	If ($retempty === FALSE) $bits = array_filter($bits); //  get rid of empty values
	return $bits;
}
function parsequery($query){
	$variables=explode("&",$query);
	$counter = count($variables)-1;
	for ($i=0;$i<=$counter;$i++){
		$tab=explode("=",$variables[$i]);
		$bits[$tab[0]]=$tab[1];
	}
	return $bits;
}

//http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
/* // this returns:
Array
(
    [original] => http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
    [scheme] => http
    [host] => example.com
    [port] => 8800
    [user] => username
    [pass] => password
    [path] => /source/test/index.html
    [dirname] => /source/test
    [basename] => index.html
    [extension] => html
    [fragment] => lots
    [query] => add=http://www.example.com/blah#location&test=89
    [add] => http://www.example.com/blah#location
    [test] => 89
) 
*/
//echo '<pre>';
//$url = 'http://www.example.com:8800/source/test/#lots?add=http://www.example.com/blah#location&test=89';
//$url = 'http://www.google.com/test/search/s2?q=ext&sourceid=opera&num=0&ie=utf-8&oe=utf-8';
//$url = 'Mailto:[email protected][email protected]&subject=Ha!&X-Mailer=Baluba&body=Ha!%0D%0ABla!';
//$bits = ext_parse_url($url,TRUE, FALSE); // TRUE od FALSE indicates whether or not to parse the query string (default = FALSE)
//print_r($bits); 
?>

Revision: 45266
at April 28, 2011 01:19 by tasmanweb


Initial Code
/* 
Extended parse_url
 parses a url correctly even with a url as query parameter
 also retuns dirname, basename and extension besides the other parse_url bits
 if $parsequery is set to TRUE the query variables and their values are returned as part of the array, this also applies to  mailto links
 also correctly parses mailto links with the email showing up as the path 
 note to self: remember extension does not include the point
 if $retempty is set to true empty values will be returned avoiding "nonexistand index" errors, if false then empty values will be erased
 Also returns isdirectory indicating whether the url points to a file or directory
 */
function parseurl($url,$retempty=TRUE,$parsequery=FALSE) {
	// $url_default determines the order of the  array elements adjust to your needs, any key not mentioned in it gets stuck on the end, rearrange to suit your needs
	// this empty array is also used in line 36 in order to avoid "nonexistant index" errors.
	$url_default = array( 
	"original" => "",
	"scheme" => "", 
	"host" => "", 
	"port" => "", 
	"user" => "", 
	"pass" => "",
	"isdirectory" => "",
	"path" => "",
	"dirname" => "",
	"basename" => "",
	"extension" => "",
	"fragment" => "",
	"query" => "" 
	);
	$qpos = strpos($url, '?'); 
	if (!empty($qpos)) {	
		$baseurl = substr($url, 0, $qpos);		
		$bits = @parse_url($baseurl);

		$bits['basename'] = @basename($bits['path']);	
		$bits['query'] = substr($url, $qpos+1);
	} else { 
		$bits = @parse_url($url);		
	}
	$bits = array_merge($url_default,$bits); // avoid non existend index errors 
	if (strtolower($bits['scheme']) == 'mailto') {
		unset($bits['basename']); // no need for basename as it duplicates the path value, just confuses things
	} else {
		$bits = array_merge($bits,pathinfo($bits['path'])); // no need to do this for mailto's
	}
	if (!empty($bits['path'])){
		if ($bits['path'][strlen($bits['path']) - 1] == '/'){
			$bits['isdirectory'] = "TRUE";
		} else { $bits['isdirectory'] = "FALSE";}
	}
	if ($bits['dirname'] == '\\') $bits['dirname'] = '/';
	$bits['original']=$url; // add the original url to the array, comment this line to leave it out
	if ($parsequery === TRUE) $bits = array_merge($bits,parsequery($bits['query']));
	
	$bits = array_merge($url_default,$bits); //set the order of elements
	If ($retempty === FALSE) $bits = array_filter($bits); //  get rid of empty values
	return $bits;
}
function parsequery($query){
	$variables=explode("&",$query);
	$counter = count($variables)-1;
	for ($i=0;$i<=$counter;$i++){
		$tab=explode("=",$variables[$i]);
		$bits[$tab[0]]=$tab[1];
	}
	return $bits;
}

//http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
//Array
//(
//    [original] => http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
//    [scheme] => http
 //   [host] => example.com
 //   [port] => 8800
//    [user] => username
//    [pass] => password
//    [path] => /source/test/index.html
//    [dirname] => /source/test
//    [basename] => index.html
//    [extension] => html
//    [fragment] => lots
//    [query] => add=http://www.example.com/blah#location&test=89
//    [add] => http://www.example.com/blah#location
//    [test] => 89
//)

//echo '<pre>';
//$url = 'http://www.example.com:8800/source/test/#lots?add=http://www.example.com/blah#location&test=89';
//$url = 'http://www.google.com/test/search/s2?q=ext&sourceid=opera&num=0&ie=utf-8&oe=utf-8';
//$url = 'Mailto:[email protected][email protected]&subject=Ha!&X-Mailer=Baluba&body=Ha!%0D%0ABla!';
//$bits = ext_parse_url($url,TRUE, FALSE); // TRUE od FALSE indicates whether or not to parse the query string (default = FALSE)
//print_r($bits);

Initial URL

                                

Initial Description
Parses a url correctly even with a url as query parameter,  also retuns dirname, basename and extension besides the other parse_url bits. See comments for more info.

Initial Title
Extended parse_url: parses a url correctly even with a url as query parameter

Initial Tags
url, php, function

Initial Language
PHP