28

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

0

8 Answers 8

38

use following

<?php
    $link = $_SERVER['PHP_SELF'];
    $link_array = explode('/',$link);
    echo $page = end($link_array);
?>
1
  • Didnt work cause my link ended up with a / example : https/mysite.com/products/my-name/ I tried the solution of @jaydeep here with "basename" it's faster and works perfectly !
    – maiakd
    Dec 14, 2022 at 9:15
36

Use basename function

echo basename("http://domain/fotografo/admin/gallery_bg.php");
3
  • 1
    this is very efficient and fast
    – farhang
    Sep 29, 2018 at 19:35
  • @farhang it depends what you want it to do. For me $_SERVER["REQUEST_URI"] was what I needed .....
    – Thanasis
    Jun 13, 2022 at 9:51
  • Worked for me as the validated answer did not because my link ended up with a / while I needed the last element whether there is one or not. Thank you !
    – maiakd
    Dec 14, 2022 at 9:17
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);
1
  • If you write URI as "https//server/myscript and it is redirected with .htaccess to lets say to https//server/some.php the only that will work and give you "myscript" is the $_SERVER["REQUEST_URI"], the other suggestions will return "some.php"
    – Thanasis
    Jun 13, 2022 at 9:50
7
 $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 
6

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

1
$url  = $_SERVER["PHP_SELF"];
$path = explode("/", $url); 
$last = end($path);
1

Try this:

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");

- Thanks

0
    $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, '/');

In PHP 7 the accepted solution is giving me the error that only variables are allowed in explode so this works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.