4 * URL constants as defined in the PHP Manual under "Constants usable with
7 * @see http://us2.php.net/manual/en/http.constants.php#http.constants.url
9 if (!defined('HTTP_URL_REPLACE')) {
10 define('HTTP_URL_REPLACE', 1);
12 if (!defined('HTTP_URL_JOIN_PATH')) {
13 define('HTTP_URL_JOIN_PATH', 2);
15 if (!defined('HTTP_URL_JOIN_QUERY')) {
16 define('HTTP_URL_JOIN_QUERY', 4);
18 if (!defined('HTTP_URL_STRIP_USER')) {
19 define('HTTP_URL_STRIP_USER', 8);
21 if (!defined('HTTP_URL_STRIP_PASS')) {
22 define('HTTP_URL_STRIP_PASS', 16);
24 if (!defined('HTTP_URL_STRIP_AUTH')) {
25 define('HTTP_URL_STRIP_AUTH', 32);
27 if (!defined('HTTP_URL_STRIP_PORT')) {
28 define('HTTP_URL_STRIP_PORT', 64);
30 if (!defined('HTTP_URL_STRIP_PATH')) {
31 define('HTTP_URL_STRIP_PATH', 128);
33 if (!defined('HTTP_URL_STRIP_QUERY')) {
34 define('HTTP_URL_STRIP_QUERY', 256);
36 if (!defined('HTTP_URL_STRIP_FRAGMENT')) {
37 define('HTTP_URL_STRIP_FRAGMENT', 512);
39 if (!defined('HTTP_URL_STRIP_ALL')) {
40 define('HTTP_URL_STRIP_ALL', 1024);
43 if (!function_exists('http_build_url')) {
48 * The parts of the second URL will be merged into the first according to
51 * @param mixed $url (part(s) of) an URL in form of a string or
52 * associative array like parse_url() returns
53 * @param mixed $parts same as the first argument
54 * @param int $flags a bitmask of binary or'ed HTTP_URL constants;
55 * HTTP_URL_REPLACE is the default
56 * @param array $new_url if set, it will be filled with the parts of the
57 * composed url like parse_url() would return
60 function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = array())
62 is_array($url) || $url = parse_url($url);
63 is_array($parts) || $parts = parse_url($parts);
65 isset($url['query']) && is_string($url['query']) || $url['query'] = null;
66 isset($parts['query']) && is_string($parts['query']) || $parts['query'] = null;
68 $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
70 // HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags.
71 if ($flags & HTTP_URL_STRIP_ALL) {
72 $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS
73 | HTTP_URL_STRIP_PORT | HTTP_URL_STRIP_PATH
74 | HTTP_URL_STRIP_QUERY | HTTP_URL_STRIP_FRAGMENT;
75 } elseif ($flags & HTTP_URL_STRIP_AUTH) {
76 $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS;
79 // Schema and host are alwasy replaced
80 foreach (array('scheme', 'host') as $part) {
81 if (isset($parts[$part])) {
82 $url[$part] = $parts[$part];
86 if ($flags & HTTP_URL_REPLACE) {
87 foreach ($keys as $key) {
88 if (isset($parts[$key])) {
89 $url[$key] = $parts[$key];
93 if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
94 if (isset($url['path']) && substr($parts['path'], 0, 1) !== '/') {
95 // Workaround for trailing slashes
98 str_replace(basename($url['path']), '', $url['path']),
100 ) . '/' . ltrim($parts['path'], '/');
102 $url['path'] = $parts['path'];
106 if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
107 if (isset($url['query'])) {
108 parse_str($url['query'], $url_query);
109 parse_str($parts['query'], $parts_query);
111 $url['query'] = http_build_query(
112 array_replace_recursive(
118 $url['query'] = $parts['query'];
123 if (isset($url['path']) && $url['path'] !== '' && substr($url['path'], 0, 1) !== '/') {
124 $url['path'] = '/' . $url['path'];
127 foreach ($keys as $key) {
128 $strip = 'HTTP_URL_STRIP_' . strtoupper($key);
129 if ($flags & constant($strip)) {
136 if (!empty($url['scheme'])) {
137 $parsed_string .= $url['scheme'] . '://';
140 if (!empty($url['user'])) {
141 $parsed_string .= $url['user'];
143 if (isset($url['pass'])) {
144 $parsed_string .= ':' . $url['pass'];
147 $parsed_string .= '@';
150 if (!empty($url['host'])) {
151 $parsed_string .= $url['host'];
154 if (!empty($url['port'])) {
155 $parsed_string .= ':' . $url['port'];
158 if (!empty($url['path'])) {
159 $parsed_string .= $url['path'];
162 if (!empty($url['query'])) {
163 $parsed_string .= '?' . $url['query'];
166 if (!empty($url['fragment'])) {
167 $parsed_string .= '#' . $url['fragment'];
172 return $parsed_string;