Advertisement
diopralinato

http_build_url_PHP

Apr 27th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2.     if (!function_exists('http_build_url'))
  3.     {
  4.         define('HTTP_URL_REPLACE', 1);              // Replace every part of the first URL when there's one of the second URL
  5.         define('HTTP_URL_JOIN_PATH', 2);            // Join relative paths
  6.         define('HTTP_URL_JOIN_QUERY', 4);           // Join query strings
  7.         define('HTTP_URL_STRIP_USER', 8);           // Strip any user authentication information
  8.         define('HTTP_URL_STRIP_PASS', 16);          // Strip any password authentication information
  9.         define('HTTP_URL_STRIP_AUTH', 32);          // Strip any authentication information
  10.         define('HTTP_URL_STRIP_PORT', 64);          // Strip explicit port numbers
  11.         define('HTTP_URL_STRIP_PATH', 128);         // Strip complete path
  12.         define('HTTP_URL_STRIP_QUERY', 256);        // Strip query string
  13.         define('HTTP_URL_STRIP_FRAGMENT', 512);     // Strip any fragments (#identifier)
  14.         define('HTTP_URL_STRIP_ALL', 1024);         // Strip anything but scheme and host
  15.        
  16.         // Build an URL
  17.         // The parts of the second URL will be merged into the first according to the flags argument.
  18.         //
  19.         // @param   mixed           (Part(s) of) an URL in form of a string or associative array like parse_url() returns
  20.         // @param   mixed           Same as the first argument
  21.         // @param   int             A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
  22.         // @param   array           If set, it will be filled with the parts of the composed url like parse_url() would return
  23.         function http_build_url($url, $parts=array(), $flags=HTTP_URL_REPLACE, &$new_url=false)
  24.         {
  25.             $keys = array('user','pass','port','path','query','fragment');
  26.            
  27.             // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
  28.             if ($flags & HTTP_URL_STRIP_ALL)
  29.             {
  30.                 $flags |= HTTP_URL_STRIP_USER;
  31.                 $flags |= HTTP_URL_STRIP_PASS;
  32.                 $flags |= HTTP_URL_STRIP_PORT;
  33.                 $flags |= HTTP_URL_STRIP_PATH;
  34.                 $flags |= HTTP_URL_STRIP_QUERY;
  35.                 $flags |= HTTP_URL_STRIP_FRAGMENT;
  36.             }
  37.             // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
  38.             else if ($flags & HTTP_URL_STRIP_AUTH)
  39.             {
  40.                 $flags |= HTTP_URL_STRIP_USER;
  41.                 $flags |= HTTP_URL_STRIP_PASS;
  42.             }
  43.            
  44.             // Parse the original URL
  45.             $parse_url = parse_url($url);
  46.            
  47.             // Scheme and Host are always replaced
  48.             if (isset($parts['scheme']))
  49.                 $parse_url['scheme'] = $parts['scheme'];
  50.             if (isset($parts['host']))
  51.                 $parse_url['host'] = $parts['host'];
  52.            
  53.             // (If applicable) Replace the original URL with it's new parts
  54.             if ($flags & HTTP_URL_REPLACE)
  55.             {
  56.                 foreach ($keys as $key)
  57.                 {
  58.                     if (isset($parts[$key]))
  59.                         $parse_url[$key] = $parts[$key];
  60.                 }
  61.             }
  62.             else
  63.             {
  64.                 // Join the original URL path with the new path
  65.                 if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH))
  66.                 {
  67.                     if (isset($parse_url['path']))
  68.                         $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
  69.                     else
  70.                         $parse_url['path'] = $parts['path'];
  71.                 }
  72.                
  73.                 // Join the original query string with the new query string
  74.                 if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY))
  75.                 {
  76.                     if (isset($parse_url['query']))
  77.                         $parse_url['query'] .= '&' . $parts['query'];
  78.                     else
  79.                         $parse_url['query'] = $parts['query'];
  80.                 }
  81.             }
  82.                
  83.             // Strips all the applicable sections of the URL
  84.             // Note: Scheme and Host are never stripped
  85.             foreach ($keys as $key)
  86.             {
  87.                 if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
  88.                     unset($parse_url[$key]);
  89.             }
  90.            
  91.            
  92.             $new_url = $parse_url;
  93.            
  94.             return
  95.                  ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '')
  96.                 .((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') .'@' : '')
  97.                 .((isset($parse_url['host'])) ? $parse_url['host'] : '')
  98.                 .((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '')
  99.                 .((isset($parse_url['path'])) ? $parse_url['path'] : '')
  100.                 .((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '')
  101.                 .((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '')
  102.             ;
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement