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?
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):
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.
hi, You can do this with httaccess mod rewrite RewriteRule /(.*)/(.*)$ index.php?folder=$1&subfolder=$2 [L]
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):