我想要获取URL中的最后一个路径片段:
http://blabla/bla/wce/news.php
或者http://blabla/blablabla/dut2a/news.php
例如,在这两个URL中,我想要获取路径片段:'wce' 和 'dut2a'。
我尝试使用 $_SERVER['REQUEST_URI']
,但是我获取到了整个URL路径。
我想要获取URL中的最后一个路径片段:
http://blabla/bla/wce/news.php
或者http://blabla/blablabla/dut2a/news.php
例如,在这两个URL中,我想要获取路径片段:'wce' 和 'dut2a'。
我尝试使用 $_SERVER['REQUEST_URI']
,但是我获取到了整个URL路径。
试试这个:
$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
假设$tokens
至少有两个元素。
试试这个:
function getLastPathSegment($url) {
$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
$pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
if (substr($path, -1) !== '/') {
array_pop($pathTokens);
}
return end($pathTokens); // get the last segment
}
echo getLastPathSegment($_SERVER['REQUEST_URI']);
我还用评论中提供的一些URL进行了测试。我必须假设所有路径都以斜杠结尾,因为我无法确定/bob是目录还是文件。除非它也有尾随斜杠,否则这将假定它是一个文件。
echo getLastPathSegment('http://server.com/bla/wce/news.php'); // wce
echo getLastPathSegment('http://server.com/bla/wce/'); // wce
echo getLastPathSegment('http://server.com/bla/wce'); // bla
parse_url
д»…иҺ·еҸ–URLи·Ҝеҫ„еҖјжҳҜжӯЈзЎ®зҡ„пјҢдҪҶдҪ д№ҹйңҖиҰҒеҜ№$_SERVER['REQUEST_URI']
жү§иЎҢзӣёеҗҢж“ҚдҪңгҖӮ - Gumbo$_SERVER['REQUEST_URI']
总是返回 something/like/this
这样的东西? - alex/bla/wce/news.php
这样的情况下,它不会像OP想要的那样返回wce
,而是返回news.php
。它应该返回倒数第二个元素而不是end()吧?我没有真正测试过,因为它需要PHP版本>=5.1.2,所以如果我有误请指正。 - andyk很容易
<?php
echo basename(dirname($url)); // if your url/path includes a file
echo basename($url); // if your url/path does not include a file
?>
basename
函数返回路径中最后一个名称组件(文件名或目录名)dirname
函数返回父目录的路径http://www.example.com/dir1/dir2/foo?redirect=http://anothersite.com/default.aspx
的情况下失败。 - andyk试试这个:
$parts = explode('/', 'your_url_here');
$last = end($parts);
另一个解决方案:
$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);
1:获取最后一个斜杠的位置 2:获取最后一个斜杠和字符串结尾之间的子字符串
看这里:测试
parse_url()
(它不适用于相对URL)。$url = 'http://aplicaciones.org/wp-content/uploads/2011/09/skypevideo-500x361.jpg?arg=value#anchor';
print_r(parse_url($url));
$url_path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $url_path);
$last = end($parts);
echo $last;
完整的代码示例在这里:http://codepad.org/klqk5o29
$arr = explode("/", $uri);
我写了一个小函数来获取url的最后一级目录/文件夹。它只适用于真实存在的url,而不是理论上的url。在我的情况下,这总是成立的,所以...
function uf_getLastDir($sUrl)
{
$sPath = parse_url($sUrl, PHP_URL_PATH); // parse URL and return only path component
$aPath = explode('/', trim($sPath, '/')); // remove surrounding "/" and return parts into array
end($aPath); // last element of array
if (is_dir($sPath)) // if path points to dir
return current($aPath); // return last element of array
if (is_file($sPath)) // if path points to file
return prev($aPath); // return second to last element of array
return false; // or return false
}
我也这么做了!享受吧!还要向之前的答案表示赞美!!!
$url = 'http://blabla/blablabla/dut2a/news.php';
$url = preg_replace('~.*/~', '', $url);
会给予
news.php
http://www.example.com/dir1/dir2/foo?redirect=http://anothersite.com/default.aspx
- eis