/ と / の間の URL の最後の文字列コンテンツを取得する必要があります
例えば:
http://mydomain.com/get_this/
or
http://mydomain.com/lists/get_this/
get_this が URL のどこにあるかを取得する必要があります。
/ と / の間の URL の最後の文字列コンテンツを取得する必要があります
例えば:
http://mydomain.com/get_this/
or
http://mydomain.com/lists/get_this/
get_this が URL のどこにあるかを取得する必要があります。
使用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;
?>
末尾にスラッシュが常にあると仮定すると、次のようになります。
$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];
このようなものがうまくいくはずです。
<?php
$subject = "http://mydomain.com/lists/get_this/";
$pattern = '/\/([^\/]*)\/$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
これを試すことができます:
preg_match("/http:\/\/([a-z0-9\.]+)\/(.+)\/(.*)\/?/", $url, $matches);
print_r($matches);