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 09:19

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

查看更多
甜甜的少女心
3楼-- · 2019-03-19 09:24
    $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.

查看更多
登录 后发表回答