web-dev-qa-db-ja.com

URLの最後のパスを取得する方法は?

URLの最後のパスセグメントを取得したい:

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

たとえば、これら2つのURLで、パスセグメント「wce」と「dut2a」を取得します。

$_SERVER['REQUEST_URI']を使用しようとしましたが、URLパス全体を取得しました。

27
bahamut100

試してください:

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

$tokensには少なくとも2つの要素があります。

51
Bart Kiers

これを試して:

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
26
alex

簡単です

<?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

22
Stepan Suvorov

これを試して:

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

絶対URLを処理する場合は、 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

1
julianm

別の解決策:

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

1:最後のスラッシュ位置の取得2:最後のスラッシュと文字列の終わりの間の部分文字列の取得

ここを見てください: [〜#〜] test [〜#〜]

1
Briganti

explode 関数を使用します

0
iBiryukov
$arr =  explode("/", $uri);
0
Gabe

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
}

私のために働く!楽しい!そして、以前の答えに拍手!

0
maxpower9000