/ Published in: PHP
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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. "original" => "", "scheme" => "", "host" => "", "port" => "", "user" => "", "pass" => "", "isdirectory" => "", "path" => "", "dirname" => "", "basename" => "", "extension" => "", "fragment" => "", "query" => "" ); } else { } unset($bits['basename']); // no need for basename as it duplicates the path value, just confuses things } else { } $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 return $bits; } function parsequery($query){ for ($i=0;$i<=$counter;$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); ?>