Php 如何获取URL中的最后一个路径?

Php 如何获取URL中的最后一个路径?,php,url-parsing,Php,Url Parsing,我想获取URL中的最后一个路径段: http://blabla/bla/wce/news.php或 http://blabla/blablabla/dut2a/news.php 例如,在这两个URL中,我想要得到路径段:“wce”和“dut2a” 我试图使用$\u SERVER['REQUEST\u URI'],但我得到了整个URL路径。尝试: $url = 'http://blabla/blablabla/dut2a/news.php'; $tokens = explode('/', $u

我想获取URL中的最后一个路径段:

  • http://blabla/bla/wce/news.php
  • http://blabla/blablabla/dut2a/news.php
例如,在这两个URL中,我想要得到路径段:“wce”和“dut2a”

我试图使用
$\u SERVER['REQUEST\u URI']
,但我得到了整个URL路径。

尝试:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
假设
$tokens
至少有2个元素。

尝试以下操作:

$arr =  explode("/", $uri);
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']);
 $parts = explode('/', 'your_url_here');
 $last = end($parts);
我还使用评论中的一些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
试试这个:

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']);
 $parts = explode('/', 'your_url_here');
 $last = end($parts);
这很容易

<?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
    将返回父目录的路径

另一种解决方案:

$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);
1:获取最后一个斜杠位置 2:获取最后一个斜杠和字符串结尾之间的子字符串


请看这里:

如果您想处理绝对URL,那么您可以使用(它不适用于相对URL)


这里的完整代码示例:

我自己编写了一个小函数来获取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


巴哈穆特100,如果这是正确的答案,那么您应该通过单击它旁边的白色复选标记来接受它。@巴哈穆特100,不客气。虽然我同意(并接受)亚历克斯的回答,但它不会处理:http://foobar.com/path/path#fragment/fragment@ddimitrov:是的,说得好。更多的理由让OP采用Alex的解决方案。但无法删除我的答案,因为它是可接受的答案…碎片并不是真正的问题,因为它们不会遗留到服务器。然而,像这样的url将是一个问题:
http://www.example.com/dir1/dir2/foo?redirect=http://anothersite.com/default.as‌​px
+1,用于使用
解析url
仅获取url路径。但是你也需要用
$\u服务器['REQUEST\u URI']
来做这件事。我想
$\u服务器['REQUEST\u URI']
总是带着
之类的东西/这个
?@alex:不,在PHP中它也包含查询。所以URL路径和URL查询(就像HTTP请求行一样)。@gumbo谢谢你的提示,我也为它做了一个函数包装器。类似这样:
/bla/wce/news.php
它不会返回
news.php
而不是像OP想要的那样返回
wce
?它不应该返回最后一个元素而不是end()?我并没有真正测试它,因为它需要PHP版本>=5.1.2,所以cmiiw.在此之前可以对url使用一些过滤。将在类似于
http://www.example.com/dir1/dir2/foo?redirect=http://anothersite.com/default.aspx
我只是用basename()解决了这个问题,以您的方式,我在“retro”
basename($\u SERVER['PHP\u SELF'])中获得了第二级是为了me@insign如果URL后面有斜杠,OP askedIt可能会失败。这只是解决方案的一半,而只有代码的答案在Stackoverflow上的值很低。@mickmackusa好吧,你参加聚会大约晚了10年。有用的反馈。我的志愿服务不因时间而有所区别。旧的低价值帖子会被新访问者阅读,并鼓励新的低价值帖子。我希望这个地方不断改进——不管内容有多旧。请对这篇文章做些什么。