-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
  • 50,627
  • 10
  • 109
  • 97
1
$url = "http://twitter.com/what/";
echo basename($url);
Shakti Singh
  • 81,083
  • 20
  • 131
  • 150
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
  • 30,367
  • 4
  • 31
  • 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
  • 43,457
  • 15
  • 77
  • 89