Break down URL
-
This is foxing me a little and I wonder if someone could point me in the right direction please or include what is needed to resolve this. My page at the moment only includes the following and the address(URL) is given as the example.
<?php
//As an example the address is taken as http://www.newusb.co.uk/jh6k95/
$url = $_SERVER['REQUEST_URI'];
//Show me the folder name
echo $url;
?>The return is... /jh6k95/index.php However the only thing I want from that is... jh6k95 So, I suppose the question is how do I strip out folder name. Many thanks in advance for any help. Ray
-
This is foxing me a little and I wonder if someone could point me in the right direction please or include what is needed to resolve this. My page at the moment only includes the following and the address(URL) is given as the example.
<?php
//As an example the address is taken as http://www.newusb.co.uk/jh6k95/
$url = $_SERVER['REQUEST_URI'];
//Show me the folder name
echo $url;
?>The return is... /jh6k95/index.php However the only thing I want from that is... jh6k95 So, I suppose the question is how do I strip out folder name. Many thanks in advance for any help. Ray
There are several ways to do it, so these are the most simple options that I can think of. 1. Use dirname[^] to get the parent directory name (if you are sure that the URL always contains the filename):
<?php
$url = $_SERVER['REQUEST_URI'];
$dir = trim(dirname($url), '/');2. Use explode[^] to break the path into an array:
<?php
$url = $_SERVER['REQUEST_URI'];
$parts = explode('/', trim($url, '/'));
$dir = $parts[0];I've used trim[^] in both of these to remove leading and trailing '/' characters.
-
There are several ways to do it, so these are the most simple options that I can think of. 1. Use dirname[^] to get the parent directory name (if you are sure that the URL always contains the filename):
<?php
$url = $_SERVER['REQUEST_URI'];
$dir = trim(dirname($url), '/');2. Use explode[^] to break the path into an array:
<?php
$url = $_SERVER['REQUEST_URI'];
$parts = explode('/', trim($url, '/'));
$dir = $parts[0];I've used trim[^] in both of these to remove leading and trailing '/' characters.
Thank You Graham, The first one only gives me \ for some reason but the question is answered with the second one you gave. Many Thanks Regards Ray