-3

Possible Duplicate:
How to get the last path in the url?

I'm not very good with regex as I haven't yet come to understand how it fully works to get the right results. I need a regex expression or php function to strip out the beginning of a url to get the last bit without the variables on it.

Input : http: // www.site.com/more/more/thepartidneed?part=Idontneed

Other Input : www.site.com/more/more/thepartineed?partidontneed

Other input : site.com/more/more/partineed

Results Desired After Stripped : "thepartineed"

Is this possible?

Community
  • 1
  • 1
Darius
  • 1,613
  • 5
  • 29
  • 58
  • 1
    lots more duplicates: http://stackoverflow.com/search?q=get+last+path+in+url+php - please use the search function before asking questions. [We expect you to do research.](http://stackoverflow.com/questions/ask-advice) – Gordon Sep 02 '12 at 12:28
  • 1
    I apologize, I couldn't come up with the word "path". :\ – Darius Sep 02 '12 at 12:29
  • 1
    See: [parse_url()](http://us2.php.net/manual/en/function.parse-url.php). Should be easy from there... – ircmaxell Sep 02 '12 at 12:30

2 Answers2

2

First you need to check if given URL contains http(s):// at the beginning, if not - append it and then do

$parsed_url = parse_url($url);
var_dump($parsed_url);

You will get something like this:

array(3) {
  ["scheme"]=>
  string(4) "http"
  ["host"]=>
  string(8) "site.com"
  ["path"]=>
  string(20) "/more/more/partineed"
}

If you need only last bit of path you can do

echo array_pop(explode('/', $parsed_url['path']));
Alexander Larikov
  • 2,328
  • 15
  • 15
1

Assuming input is always some sort of url:

.+/([^?]+).*

Your part should be in identifier 1

MDEV
  • 10,730
  • 2
  • 33
  • 49