URL - Get last part in PHP

2019-03-19 08:26发布

I have my url:

http://domain/fotografo/admin/gallery_bg.php

and i want last part of the url:

 gallery_bg.php

but, I do not want to link static, ie, for each page that vistitar I want to get the last part of the url

标签: php html regex url
8条回答
放我归山
2楼-- · 2019-03-19 08:57

you can use basename($url) function as suggested above. This returns the file name from the url. You can also provide the file extension as second argument to this function like basename($url, '.jpg'), then the filename without the extension will be served.

Eg:

$url = "https://i0.com/images/test.jpg"

then echo basename($url) will print test.jpg

and echo basename($url,".jpg") will print test

查看更多
看我几分像从前
3楼-- · 2019-03-19 09:05

Use basename function

echo basename("http://domain/fotografo/admin/gallery_bg.php");
查看更多
一纸荒年 Trace。
4楼-- · 2019-03-19 09:06
 $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 
查看更多
Anthone
5楼-- · 2019-03-19 09:11

If it is same page:

echo $_SERVER["REQUEST_URI"];

or

echo $_SERVER["SCRIPT_NAME"];

or 

echo $_SERVER["PHP_SELF"];

In each case a back slash(/gallery_bg.php) will appear. You can trim it as

echo trim($_SERVER["REQUEST_URI"],"/");

or split the url by / to make an array and get the last item from array

$array = explode("/",$url);

$last_item_index = count($url) - 1;

echo $array[$last_item_index];

or

echo basename($url);
查看更多
老娘就宠你
6楼-- · 2019-03-19 09:11
$url  = $_SERVER["PHP_SELF"];
$path = explode("/", $url); 
$last = end($path);
查看更多
放我归山
7楼-- · 2019-03-19 09:12

use following

<?php
    $link = $_SERVER['PHP_SELF'];
    $link_array = explode('/',$link);
    echo $page = end($link_array);
?>
查看更多
登录 后发表回答