Public Member Functions | |
absUrl ($base_, $url_) | |
Transform relative uri into target uri, rfc3986 compatible. | |
Private Member Functions | |
_removeDotSegments ($path) | |
_merge ($Bpath, $Bauthority, $Rpath) |
These functions are based on http://www.chrsen.dk/fundanemt/files/scripter/php/misc/rfc3986.php
Definition at line 12 of file Url.class.php.
Url::_merge | ( | $ | Bpath, | |
$ | Bauthority, | |||
$ | Rpath | |||
) | [private] |
Definition at line 194 of file Url.class.php.
Referenced by absUrl().
00195 { 00196 if ( isset($Bauthority) && !empty($Bauthority) && empty($Bpath) ) 00197 { 00198 return "/" . $Rpath; 00199 } 00200 else 00201 { 00202 return preg_replace("/[^\/]*$/","",$Bpath) . $Rpath; 00203 } 00204 }
Url::_removeDotSegments | ( | $ | path | ) | [private] |
Definition at line 121 of file Url.class.php.
Referenced by absUrl().
00122 { 00123 /* 00124 1. The input buffer is initialized with the now-appended path 00125 components and the output buffer is initialized to the empty 00126 string. 00127 */ 00128 $bInput = $path; 00129 $bOutput = ""; 00130 /* 00131 2. While the input buffer is not empty, loop as follows: 00132 */ 00133 while( !empty($bInput) ) 00134 { 00135 /* 00136 A. If the input buffer begins with a prefix of "../" or "./", 00137 then remove that prefix from the input buffer; otherwise, 00138 */ 00139 if ( preg_match("/^(\.\.\/|\.\/)/", $bInput, $match) ) 00140 { 00141 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","",$bInput); 00142 } 00143 /* 00144 B. if the input buffer begins with a prefix of "/./" or "/.", 00145 where "." is a complete path segment, then replace that 00146 prefix with "/" in the input buffer; otherwise, 00147 */ 00148 else if ( preg_match("/^(\/\.\/|\/\.$)/",$bInput, $match) ) 00149 { 00150 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","/",$bInput); 00151 } 00152 00153 /* 00154 C. if the input buffer begins with a prefix of "/../" or "/..", 00155 where ".." is a complete path segment, then replace that 00156 prefix with "/" in the input buffer and remove the last 00157 segment and its preceding "/" (if any) from the output 00158 buffer; otherwise, 00159 */ 00160 else if ( preg_match("/^(\/\.\.\/|\/\.\.$)/",$bInput, $match) ) 00161 { 00162 $bInput = preg_replace ( "/^" . preg_quote($match[0], "/") . "/","/",$bInput); 00163 $bOutput = preg_replace ( "/\/?[^\/]+$/", "", $bOutput); 00164 } 00165 /* 00166 D. if the input buffer consists only of "." or "..", then remove 00167 that from the input buffer; otherwise, 00168 */ 00169 else if ( preg_match("/^(\.\.|\.)$/",$bInput, $match) ) 00170 { 00171 $bInput = preg_replace("/^" . preg_quote($match[1], "/") . "/","",$bInput); 00172 } 00173 /* 00174 E. move the first path segment in the input buffer to the end of 00175 the output buffer, including the initial "/" character (if 00176 any) and any subsequent characters up to, but not including, 00177 the next "/" character or the end of the input buffer. 00178 */ 00179 else 00180 { 00181 preg_match("/^\/?[^\/]*/",$bInput,$match); 00182 $bInput = preg_replace("/^" . preg_quote($match[0], "/") . "/","",$bInput); 00183 $bOutput .= $match[0]; 00184 } 00185 }//while 00186 /* 00187 3. Finally, the output buffer is returned as the result of 00188 removeDotSegments. 00189 */ 00190 return $bOutput; 00191 }
Url::absUrl | ( | $ | base_, | |
$ | url_ | |||
) |
Transform relative uri into target uri, rfc3986 compatible.
Read more at <http://rfc.net/rfc3986.html> section 5.2
// Will give http://cybercow.se/php/ex/error/index.php $url = url::absUrl("http://cybercow.se/php/ex/absurl/index.php", "../error/index.php")
Definition at line 23 of file Url.class.php.
References _merge(), and _removeDotSegments().
00024 { 00025 static $urlCache = array(); 00026 00027 $urlKey = md5($base_. '___' . $url_); 00028 if (!array_key_exists($urlKey, $urlCache)) 00029 { 00030 preg_match("/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/",$url_,$urlComp); 00031 list(,,$Rscheme,,$Rauthority,$Rpath,,$Rquery,,$Rfragment) = $urlComp; 00032 00033 if ( isset($Rscheme) && !empty($Rscheme) ) 00034 { 00035 $Tscheme = $Rscheme; 00036 $Tauthority = $Rauthority; 00037 $Tpath = url::_removeDotSegments($Rpath); 00038 $Tquery = $Rquery; 00039 00040 } 00041 else 00042 { 00043 if ( isset($Rauthority) && !empty($Rauthority) ) 00044 { 00045 $Tauthority = $Rauthority; 00046 $Tpath = url::_removeDotSegments($Rpath); 00047 $Tquery = $Rquery; 00048 } 00049 else 00050 { 00051 preg_match("/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/",$base_,$baseComp); 00052 list(,,$Bscheme,,$Bauthority,$Bpath,,$Bquery,,$Bfragment) = $baseComp; 00053 00054 if ( $Rpath == "") 00055 { 00056 $Tpath = $Bpath; 00057 if ( isset($Rquery) && !empty($Rquery) ) 00058 { 00059 $Tquery = $Rquery; 00060 } 00061 else 00062 { 00063 $Tquery = $Bquery; 00064 } 00065 } 00066 else 00067 { 00068 if ( preg_match("/^\//", $Rpath) ) 00069 { 00070 $Tpath = url::_removeDotSegments($Rpath); 00071 } 00072 else 00073 { 00074 $Tpath = url::_merge($Bpath,$Bauthority,$Rpath); 00075 $Tpath = url::_removeDotSegments($Tpath); 00076 }// preg_match("/^//", $Rpath) 00077 00078 $Tquery = $Rquery; 00079 }//if $Rpath == "" 00080 00081 $Tauthority = $Bauthority; 00082 }// isset($Rauthority) && !empty($Rauthority) 00083 00084 $Tscheme = $Bscheme; 00085 00086 }// isset($Rscheme) && !empty($Rscheme) 00087 00088 $Tfragment = $Rfragment; 00089 00090 $result = ""; 00091 00092 if ( isset($Tscheme) && !empty($Tscheme) ) 00093 { 00094 $result .= $Tscheme . ":"; 00095 } 00096 00097 if ( isset($Tauthority) && !empty($Tauthority) ) 00098 { 00099 $result .= "//" . $Tauthority; 00100 } 00101 00102 $result .= $Tpath; 00103 00104 if ( isset($Tquery) && !empty($Tquery) ) 00105 { 00106 $result .= "?" . $Tquery; 00107 } 00108 00109 if ( isset($Tfragment) && !empty($Tfragment) ) 00110 { 00111 $result .= "#" . $Tfragment; 00112 } 00113 00114 $urlCache[$urlKey] = $result; 00115 } 00116 00117 return $urlCache[$urlKey]; 00118 }//function