Remove the last directory from 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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Remove the last directory from URL

Post by klevis miho »

Any ideas on how to removee the last folder from a given URL. For example:
www.example.com/test1/test2/test3

I want to remove only test3
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Remove the last directory from URL

Post by timWebUK »

Perhaps a regular expression? Read any valid characters then when you come across a '/' check for any further characters?

I'm no regex expert though, so you're on your own there!
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Remove the last directory from URL

Post by klevis miho »

Thnx man.
I found it out myself using strrpos().
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Remove the last directory from URL

Post by pickle »

basename() might be the simplest.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply