我有我的URL:
http://domain/fotografo/admin/gallery_bg.php
我想要URL的最后一部分:
gallery_bg.php
但是,我不想链接静态页面,也就是说,对于每个访问者访问的页面,我想获取URL的最后一部分。
使用 basename 函数
echo basename("http://domain/fotografo/admin/gallery_bg.php");
请使用以下内容
<?php
$link = $_SERVER['PHP_SELF'];
$link_array = explode('/',$link);
echo $page = end($link_array);
?>
如果是同一页:
echo $_SERVER["REQUEST_URI"];
or
echo $_SERVER["SCRIPT_NAME"];
or
echo $_SERVER["PHP_SELF"];
在每种情况下,都会出现一个反斜杠(/
gallery_bg.php)。您可以将其删除。
echo trim($_SERVER["REQUEST_URI"],"/");
或者按照/
分割链接并将其转换为数组,然后获取数组中的最后一项。$array = explode("/",$url);
$last_item_index = count($url) - 1;
echo $array[$last_item_index];
或者echo basename($url);
$url = "http://domain/fotografo/admin/gallery_bg.php";
$keys = parse_url($url); // parse the url
$path = explode("/", $keys['path']); // splitting the path
$last = end($path); // get the value of the last element
$url = "https://i0.com/images/test.jpg"
then echo basename($url) will print test.jpg
and echo basename($url,".jpg") will print test
试试这个:
Here you have 2 options.
1. Using explode function.
$filename = end(explode('/', 'http://domain/fotografo/admin/gallery_bg.php'));
2. Use basename function.
$filename = basename("http://domain/fotografo/admin/gallery_bg.php");
- 谢谢
$url = $_SERVER["PHP_SELF"];
$path = explode("/", $url);
$last = end($path);
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
$uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
$url = trim($uri, '/');