Page 1 of 1

Get Part Of a URL

Posted: Sun Apr 25, 2010 9:49 pm
by Quazel
Hi,
I am trying to get the last slash (/xxx/) out of http://XXX.example.com/xxx/ and just appear as "xxx"

Re: Get Part Of a URL

Posted: Sun Apr 25, 2010 10:32 pm
by Luke
You're going to need to give us more information. What is this for? How does your application work? Are you talking about redirecting from /xxx/ to /xxx ? Give us more info.

Re: Get Part Of a URL

Posted: Sun Apr 25, 2010 11:00 pm
by phu
Try this: $_SERVER superglobal

You may be looking for REQUEST_URI (that's URI, not URL).

This would return 'xxx' given the example URL you provided:

Code: Select all

str_replace('/', '', $_SERVER['REQUEST_URI']);

Re: Get Part Of a URL

Posted: Mon Apr 26, 2010 2:53 pm
by tr0gd0rr
If the url is not the same as the REQUEST_URI, you can use `parse_url()` then use `trim()`

Code: Select all

$path = parse_url("http://XXX.example.com/xxx/", PHP_URL_PATH);
$path = trim($path, "/");
echo $path; // xxx