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

时间:2010-02-16 13:47:38

标签: php url-parsing

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

  • http://blabla/bla/wce/news.php
  • http://blabla/blablabla/dut2a/news.php

例如,在这两个网址中,我想获得路径段:'wce'和'dut2a'。

我尝试使用$_SERVER['REQUEST_URI'],但我获得了整个网址路径。

10 个答案:

答案 0 :(得分:51)

尝试:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];

假设$tokens至少有2个元素。

答案 1 :(得分:26)

试试这个:

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']);

我还使用评论中的一些网址对其进行了测试。我将不得不假设所有路径都以斜杠结尾,因为我无法识别/ 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

答案 2 :(得分:22)

很容易

<?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://php.net/manual/en/function.dirname.php

http://php.net/manual/en/function.basename.php

答案 3 :(得分:9)

试试这个:

 $parts = explode('/', 'your_url_here');
 $last = end($parts);

答案 4 :(得分:1)

另一种解决方案:

$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);

1:获得最后一个斜线位置 2:获取最后一个斜杠和字符串结尾之间的子串

请看这里:TEST

答案 5 :(得分:1)

如果您要处理绝对网址,则可以使用parse_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

答案 6 :(得分:0)

使用explode功能

答案 7 :(得分:0)

$arr =  explode("/", $uri);

答案 8 :(得分:0)

我自己写了一个函数来获取url的最后一个dir /文件夹。它只适用于实际/现有网址,而不是理论网址。就我而言,情况总是如此,所以......

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
}

适合我!请享用!并赞赏以前的答案!!!

答案 9 :(得分:0)

这将保留最后一个斜线之后的部分。
不用担心爆炸,例如没有斜线时。

$url = 'http://blabla/blablabla/dut2a/news.php';
$url = preg_replace('~.*/~', '', $url);

会给

news.php
相关问题