1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php url to variable question

Discussion in 'PHP' started by izlik, May 5, 2010.

  1. #1
    Hello

    I have a question about URLs and variables. Let's say my url is http://pages.com/folder/subfolder/ how can i do so that "Subfolder" becomes a $subfolder variable i can call on my page?
     
    izlik, May 5, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    You can try this method:

    
    <?php
    $url = "http://pages.com/folder/subfolder/";
    
    $url_parts = explode("/",$url);
    
    foreach($url_parts as $url_part){
        $$url_part = $url_part;
    }
    
    echo("Subfolder: ".$subfolder);
    ?>
    
    Code (markup):
     
    s_ruben, May 5, 2010 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Thanks ruben. I forgot to say, "subfolder" can change. so if they for example go to http://pages.com/folder/newspapers/ $subfolder should ofcourse be "newspapers",however if they go to http://pages.com/folder/cars/ $subfolder should then be $cars. how could this be accomplished ? Im using URL rewriting. so all these pages goes to the same php file but gives diffrent information depending on whats at the end of the url. so in short i wannat "get" either "cars" or "newspapers" or whatever is at the end there and trow it into $subfolder.
     
    Last edited: May 6, 2010
    izlik, May 5, 2010 IP
  4. Al0r

    Al0r Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi,

    You can do this with httaccess mod rewrite

    RewriteRule /(.*)/(.*)$ index.php?folder=$1&subfolder=$2 [L]
     
    Al0r, May 6, 2010 IP
  5. Al0r

    Al0r Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    and you can give option in php that e.g:

    <a href="http://pages.com/$folder/$subfolder">bla bla</a>
     
    Al0r, May 6, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    Yes, the code that I offer you does it. For example if the url is "http://pages.com/folder/newspapers/" the variable $newspapers will be "newspapers" and if the url is "http://pages.com/folder/cars/" the variable $cars will be "cars" and so on. Only change the url and try to access the variable that you need. Like this:

    
    <?php
    $url = "http://pages.com/folder/newspapers/";
    
    $url_parts = explode("/",$url);
    
    foreach($url_parts as $url_part){
        $$url_part = $url_part;
    }
    
    $url = "http://pages.com/folder/cars/";
    
    $url_parts = explode("/",$url);
    
    foreach($url_parts as $url_part){
        $$url_part = $url_part;
    }
    
    echo($newspapers."<br />");
    echo($cars);
    ?>
    
    Code (markup):
     
    s_ruben, May 6, 2010 IP