-3

Possible Duplicate:
Extract part from URL for a query string
how do i parse url php

I have a string like this one:

http://twitter.com/what/

How can I get the what part ?

Basically I want to get the last part of a URL

Community
  • 1
  • 1
Jane
  • 3
  • 1

4 Answers4

3

http://php.net/manual/en/function.parse-url.php

$url = "http://twitter.com/what/";

$parts = parse_url($url);

$what = substr($parts['path'], 1, strlen($parts['path']) - 2);
Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
1
$url = "http://twitter.com/what/";
echo basename($url);
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

Using preg_match

<?php

if (preg_match('/http:\/\/([^\/]*)\/(.*)/', "http://twitter.com/what/", $m)) {
    $domain = $m[1]; // will contain 'twitter.com'
    $path = $m[2]; // will contain 'what/'
}
arunkumar
  • 32,803
  • 4
  • 32
  • 47
1
$url = "http://twitter.com/what/";
$parts = parse_url($url);
$parts = explode('/', trim($parts['path'], '/'));

echo $parts[0]; // output: what

If the URL will contain other parts in the path. E.g. http://twitter.com/what/else/. Then the else part will be on the second index of the array, and you can access it like this $parts[1].

Shef
  • 44,808
  • 15
  • 79
  • 90