/** ************************************************ * Trims whitespace off the url, then removes the / * off the right side of the url if it exists. * Gets the base url and the url with just it's path * @param: * @return: null * @throws: ** ***********************************************/ public function setURL($url) { if (is_string($url) && WebUtils::isValidURL($url)) { $url = rtrim(trim($url), '/'); $this->sURL = $url; $base = WebUtils::getBaseURL($url); if ($base == '') { $base = $url; } $this->sBaseURL = $base; $path = WebUtils::getCurrentPathURL($url); if ($path == '') { $path = $url; } $this->sCurrentPathURL = $path; } else { throw new Exception('AnchorParser.setURL accepts valid URLs only.'); } }
/** ************************************************ * This function tries to travers the given url's path * but will not work if the realtive path is not traversable * with the given url. * @param: $url string; a url * @param: $relPath string; a relative path * @return: string; the url after it was traversed by * relPath. Or false if there was some error/problem * TODO: Do a better job of adding / in the return string ***************************************************/ public static function traversRelPath($sUrl, $relPath) { if (!WebUtils::isValidURL($sUrl)) { return false; } $baseP = WebUtils::getCurrentPathURL($sUrl); $baseU = WebUtils::getBaseURL($sUrl); $path = WebUtils::getPathOnly($sUrl); $path = explode('/', $path); $relPath = explode('/', $relPath); //echo '<pre>Path '. var_export($path, true) . '</pre><br/>'; //echo '<pre>rel '. var_export($relPath, true) . '</pre><br/>'; foreach ($relPath as $r) { if ($r != '.' && $r != '..') { array_push($path, array_shift($relPath)); continue; } else { if ($r == '.') { array_shift($relPath); continue; } else { if ($r == '..') { array_shift($relPath); if (array_pop($path) == null) { return false; } } } } } return rtrim($baseU . implode('/', $path) . implode('/', $relPath), '/'); }