Get a part of a string [duplicate]
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
开发者_C百科part ?
Basically I want to get the last part of a URL
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);
$url = "http://twitter.com/what/";
echo basename($url);
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/'
}
$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]
.
精彩评论