Posted By

tasmanweb on 04/28/11


Tagged

url php function parser


Versions (?)

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


 / 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.

  1. <?php
  2. /*
  3. Extended parse_url
  4.  parses a url correctly even with a url as query parameter
  5.  also retuns dirname, basename and extension besides the other parse_url bits
  6.  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
  7.  also correctly parses mailto links with the email showing up as the path
  8.  note to self: remember extension does not include the point
  9.  if $retempty is set to true empty values will be returned avoiding "nonexistand index" errors, if false then empty values will be erased
  10.  Also returns isdirectory indicating whether the url points to a file or directory
  11.  */
  12. function parseurl($url,$retempty=TRUE,$parsequery=FALSE) {
  13. // $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
  14. // this empty array is also used in line 36 in order to avoid "nonexistant index" errors.
  15. $url_default = array(
  16. "original" => "",
  17. "scheme" => "",
  18. "host" => "",
  19. "port" => "",
  20. "user" => "",
  21. "pass" => "",
  22. "isdirectory" => "",
  23. "path" => "",
  24. "dirname" => "",
  25. "basename" => "",
  26. "extension" => "",
  27. "fragment" => "",
  28. "query" => ""
  29. );
  30. $qpos = strpos($url, '?');
  31. if (!empty($qpos)) {
  32. $baseurl = substr($url, 0, $qpos);
  33. $bits = @parse_url($baseurl);
  34.  
  35. $bits['basename'] = @basename($bits['path']);
  36. $bits['query'] = substr($url, $qpos+1);
  37. } else {
  38. $bits = @parse_url($url);
  39. }
  40. $bits = array_merge($url_default,$bits); // avoid non existend index errors
  41. if (strtolower($bits['scheme']) == 'mailto') {
  42. unset($bits['basename']); // no need for basename as it duplicates the path value, just confuses things
  43. } else {
  44. $bits = array_merge($bits,pathinfo($bits['path'])); // no need to do this for mailto's
  45. }
  46. if (!empty($bits['path'])){
  47. if ($bits['path'][strlen($bits['path']) - 1] == '/'){
  48. $bits['isdirectory'] = "TRUE";
  49. } else { $bits['isdirectory'] = "FALSE";}
  50. }
  51. if ($bits['dirname'] == '\\') $bits['dirname'] = '/';
  52. $bits['original']=$url; // add the original url to the array, comment this line to leave it out
  53. if ($parsequery === TRUE) $bits = array_merge($bits,parsequery($bits['query']));
  54.  
  55. $bits = array_merge($url_default,$bits); //set the order of elements
  56. If ($retempty === FALSE) $bits = array_filter($bits); // get rid of empty values
  57. return $bits;
  58. }
  59. function parsequery($query){
  60. $variables=explode("&",$query);
  61. $counter = count($variables)-1;
  62. for ($i=0;$i<=$counter;$i++){
  63. $tab=explode("=",$variables[$i]);
  64. $bits[$tab[0]]=$tab[1];
  65. }
  66. return $bits;
  67. }
  68.  
  69. //http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
  70. /* // this returns:
  71. Array
  72. (
  73.   [original] => http://username:[email protected]:8800/source/test/index.html#lots?add=http://www.example.com/blah#location&test=89
  74.   [scheme] => http
  75.   [host] => example.com
  76.   [port] => 8800
  77.   [user] => username
  78.   [pass] => password
  79.   [path] => /source/test/index.html
  80.   [dirname] => /source/test
  81.   [basename] => index.html
  82.   [extension] => html
  83.   [fragment] => lots
  84.   [query] => add=http://www.example.com/blah#location&test=89
  85.   [add] => http://www.example.com/blah#location
  86.   [test] => 89
  87. )
  88. */
  89. //echo '<pre>';
  90. //$url = 'http://www.example.com:8800/source/test/#lots?add=http://www.example.com/blah#location&test=89';
  91. //$url = 'http://www.google.com/test/search/s2?q=ext&sourceid=opera&num=0&ie=utf-8&oe=utf-8';
  92. //$url = 'Mailto:[email protected]?cc=[email protected]&subject=Ha!&X-Mailer=Baluba&body=Ha!%0D%0ABla!';
  93. //$bits = ext_parse_url($url,TRUE, FALSE); // TRUE od FALSE indicates whether or not to parse the query string (default = FALSE)
  94. //print_r($bits);
  95. ?>

Report this snippet  

You need to login to post a comment.