1

/ と / の間の URL の最後の文字列コンテンツを取得する必要があります

例えば:

http://mydomain.com/get_this/

or

http://mydomain.com/lists/get_this/

get_this が URL のどこにあるかを取得する必要があります。

4

5 に答える 5

11

trim()は末尾のスラッシュを削除し、strrpos()/は(トリミングされた後に)の最後の出現を見つけ、substr()は の最後の出現の後のすべてのコンテンツを取得します/

$url = trim($url, '/');
echo substr($url, strrpos($url, '/')+1);

出力を表示


さらに良いことに、 hakre が提案したように、basename()を使用できます。

echo basename($url);

出力を表示

于 2012-06-17T22:33:33.227 に答える
1

使用parse_url()してexplode()

<?php

$url = "http://mydomain.com/lists/get_this/";
$path = parse_url($url, PHP_URL_PATH);
$path_array = array_filter(explode('/', $path));
$last_path = $path_array[count($path_array) - 1];

echo $last_path;

?>
于 2012-06-17T22:48:47.803 に答える
1

末尾にスラッシュが常にあると仮定すると、次のようになります。

$parts = explode('/', $url);
$get_this = $parts[count($parts)-2]; // -2 since there will be an empty array element due to the trailing slash

そうでない場合:

$url = trim($url, '/'); // If there is a trailing slash in this URL instance get rid of it so we're always sure the last part is where we expect it
$parts = explode('/', $url);
$get_this = $parts[count($parts)-1];
于 2012-06-17T22:33:22.937 に答える
1

このようなものがうまくいくはずです。

<?php
$subject = "http://mydomain.com/lists/get_this/";
$pattern = '/\/([^\/]*)\/$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
于 2012-06-17T22:35:18.367 に答える
0

これを試すことができます:

preg_match("/http:\/\/([a-z0-9\.]+)\/(.+)\/(.*)\/?/", $url, $matches);
print_r($matches);
于 2012-06-17T22:33:31.517 に答える