PHP Classes
Icontem

File: gUrl.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cesar D. Rodas  >  guaranix URL  >  gUrl.php  
File: gUrl.php
Role: Class source
Content type: text/plain
Description: The main class
Class: guaranix URL
Compare URLs and compute relative URIs
 

Contents

Class file image Download
<?
/*
 *************************************************************
 *    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 <saddor@guaranix.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;
    }
}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products