SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    Boston
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I separate arguments with slashes?

    How do I make an app that let's me pass argments as slash-delimited. Something like this

    Code:
    http://foo.com/somedir/myapp/arg1/arg2/arg3
    or another one

    Code:
    http://foo.com/samepath/myapp/name1/val1/name2/val2/namen/valn
    instead of

    Code:
    http://foo.com/somepath/myapp.php?name1=val1&name2=val2
    At the PHP|Works conference last year in Toronto there was a quick remark in one of the sessions about how to do it. I scribbled a note which was barely legible at the time and is completely unintelligible now.

    Anyone?

    Thanks,
    --Mark
    Last edited by gdtrfb; Jun 16, 2005 at 11:30.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By argments (arguments?) do you mean using GET, POST, or command line?
    I study speed waiting. I can wait an entire hour in 10 minutes.

  3. #3
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code will replace all ?, & and = characters with slashes:

    PHP Code:
    $initialURL="http://foo.com/somepath/myapp.php?name1=val1&name2=val2";
    $resultURL=implode("/",explode("&",implode("/",explode("=",implode("/",explode("?",$initial))))));
    // returns http://foo.com/somepath/myapp.php/name1/val1/name2/val2 
    Hope that helps.

  4. #4
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, take a look at mod_rewrite, or if that's not available:
    PHP Code:
    class adressline//emulate $_GET behaviour without using  mod_rewrite
    {
    var 
    $get$backup;

        function 
    __construct($stringFrom false$stringDelimiter false)
        {
        
            if(!
    $stringFrom){$stringFrom $_SERVER['PATH_INFO'];}
            if(!
    $stringDelimiter){$stringDelimiter '/';}
        
        
            
    //pathinfo, removes slashes at beg. and end + ".."  
            
    $pInfo   str_replace('..'''trim($_SERVER['PATH_INFO'], $stringDelimiter));
            
    $varData explode($stringDelimiter$pInfo);
      
            
    $this->get $varData;
        }
        function 
    adressline($stringFrom false$stringDelimiter false){$this->__construct($stringFrom$stringDelimiter);}   
            


            function 
    asArray($data false$explodeString false)
            {
                if(!
    $data){$data $this->get;}
                if(!
    $explodeString){$explodeString '|';}
            
                foreach(
    $data as $key => $value)
                {
                    if(
    is_array($value))
                    {
                        
    //loop through this funtion again
                        
    $this->asArray($value);
                
                    }
                    else
                    {
                        
                        
    //explode on each- and add to array
                        
    $exploded explode($explodeString $value);
                        
    $useAsKey array_shift($exploded);
                        
    /* firs value in exploded is taken out and used as key,
                        assign the rest as values to the key
                        
                        if the array contains only one new value, make $exploded a string instead
                        */
                        
                      /*  if(count($exploded) <= 1)
                        {
                            $newArray[$useAsKey] =$exploded[0];
                        
                        }
                        else
                        {*/
                            
    $newArray[$useAsKey] =$exploded;
                        
    //}
                        
                
                    
    }
                    
                }
                return 
    $newArray;
            
            }
            
            function 
    emulatedGet(/* void */)
            {
            
                return(
    $this->asArray());
                
            }

            function 
    overWriteGet()
            {
                
    $this->backup $this->emulatedGet();
                
    $_GET = array();
                
    $_GET $this->backup;
            }
            
    }       
            
    $varData = new adressline();
            
    $varData->overWriteGet();
            
    //print_r($_GET); 

  5. #5
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this class actually work? Has it been tested?

  6. #6
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Does this class actually work? Has it been tested?
    I wrote it, so I know it will work.. with one exception though.

    These will work: http://foo.tld/index.php/values
    This will not: http://foo.tld/values

  7. #7
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you mind a simple explanation of the implementation of the above code?

    How the instantiation of the class should be done, and how it can be used?

  8. #8
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Would you mind a simple explanation of the implementation of the above code?

    How the instantiation of the class should be done, and how it can be used?
    Hmm, sure. Basically, I make use of that $_SERVER['PATH_INFO'] will be different for how the php script is called. Then I strip out illiegal characters and assign keys and values based on the remaining string.

    I always want the keys to be in an array (even if there are no keys, i still want the array), thats why I have commented out some code in there.

    Download the script to webfolder/oop/adressline.php
    remove // in front of print_r ..
    and access it like so:
    http://localhost/oop/adressline.php/php or so:
    http://localhost/oop/adressline.php/php|hello/ss|ss

    Basically, "/" is a key separator, and '|' is a value separator

  9. #9
    FBI secret agent digitman's Avatar
    Join Date
    Sep 2004
    Location
    Work
    Posts
    697
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just use mod_rewrite dude

  10. #10
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by digitman
    just use mod_rewrite dude
    mod_rewrite is not always working

  11. #11
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are on apache with mod_rewrite installed, I would recommend using it.
    I personally prefer mod_rewrite over PHP methods.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •