Can you grab a folder name from a URL and pass that to a PHP variable?

Les_N00bian
Les_N00bian used Ask the Experts™
on
I'm wondering if you can grab a folder name from a URL and pass that to a PHP variable? For example, suppose I have mydomain.com/new-york/ and in that directory I have index.php and that page has a variable in it, say $cityname. Instead of setting $cityname manually, could I have it look at the folder that it resides in, "new-york", and pull that as the value for $cityname?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Yes there is a PHP variable: $_SERVER["REQUEST_URI"] that contains the URI of the current URL.  From there you can use string/regex parsing to just grab the city.  How you do this will depend on the path of your URI

Yes you can.

Just put the following script in your "new-york" folder and try it!! =P
<?php 

	$cityname = str_replace('/','',pathinfo($_SERVER['PHP_SELF'],PATHINFO_DIRNAME));
	echo  "The city Name is: $cityname";
	
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial