Location: PHPKode > scripts > guaranix URL > guaranix-url/gUrl.php
<?
/*
 *************************************************************
 *    guaranixURL                                            *
 *************************************************************
 *    The Author disclaims the copyright of this project     *
 *    You are free to do use this project for free with      *
 *    the only condition that do not forget that "Is better  *
 *    Give than Receive"                                     *
 *************************************************************
 *    Cesar Rodas <hide@address.com>                      *
 *************************************************************
*/

/*
 *    Guaranix URL, this class takes two arguments a Base and
 *    relative URL and mix it, and return a new URL. 
 */
class gURL
{
    function newURI($base=false, $rel=false)
    {
        if ($base==false || $rel==false)
        {    
            printf("Error in %s line %d\n",__FILE__,__LINE__-3);
            return;
        }
        $pBase = parse_url($base);
        $pRel  = parse_url($rel);
        

        if ( isset($pRel['scheme']) )
        {
            /*
             *    The relative URL is not relative, so return it.
             */
            $ret = $rel;
        }
        else    
        {
            $ret  = $pBase['scheme']."://".$pBase['host'];
            $ret .= isset($pBase['port']) && $pBase['port']!=80 ? ":".$pBase['port']."/" : "/";
            if ($rel[0] == "/")
            {
                $ret .= substr($rel,1);
            }
            else 
            {
                $tmp = $this->Folder2Array($pBase['path']);
                $tmp1 = $this->Folder2Array($pRel['path'],false);
                
                foreach($tmp1 as $id1 => $d1)
                    if ($d1 == "..") unset($tmp[count($tmp)-1]);
                    else if ($d1 == ".") continue;
                    else $tmp[] = $d1;
                $ret .= implode("/",$tmp);
                $ret .= substr($rel,-1,1) == "/" ? "/" : "";
                $ret .= isset($pRel['query']) ? "?".$pRel['query'] : "";
                unset($tmp,$tmp1); /* Release memory */
            }
        }
        unset($pBase,$pRel);/* Release memory */
        return $ret;
    }    
    
    function Folder2Array($path,$onlyDir=true)
    {
        if ($path[0] == '/')
            $path=substr($path,1);
        $ret = explode("/",$path);
        if ( $onlyDir && substr(trim($path),-1,1) != "/")
            /*
             * If onlyDir is true and the last letter isn't a /
             * seems that is not a Dir! so, delete it!
             */
            unset( $ret[count($ret)-1] );
        
        if ($ret[count($ret)-1]=="")
            /*
             * If the last element of $ret is empty, delete it
             */
            unset($ret[count($ret)-1]);
        return $ret;
    }
}

?>
Return current item: guaranix URL