Get Part Of a URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Quazel
Forum Commoner
Posts: 25
Joined: Thu Mar 18, 2010 7:12 pm
Location: Edina, Minnesota

Get Part Of a URL

Post by Quazel »

Hi,
I am trying to get the last slash (/xxx/) out of http://XXX.example.com/xxx/ and just appear as "xxx"
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Get Part Of a URL

Post 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.
phu
Forum Commoner
Posts: 61
Joined: Tue Mar 30, 2010 6:18 pm

Re: Get Part Of a URL

Post 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']);
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Get Part Of a URL

Post 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
Post Reply