phpfilenamestext-extractionsanitizationbasename

Get the filename (basename) value from $_SERVER['REQUEST_URI']


I have 2 links like this:
http://www.example.com/report/4fbb14
http://www.example.com/4fbb14

so $_SERVER['REQUEST_URI'] for them is like:

/report/4fbb14
/4fbb14

I just need to get 4fbb14

I have used

$r_url = str_replace("/","",$_SERVER['REQUEST_URI']);

But I need it to work for both links, is something like this acceptable?

$r_url = str_replace("(/ or report)","",$_SERVER['REQUEST_URI']);

Solution

  • Much simpler would be

    $r_url = end(explode('/', $_SERVER['REQUEST_URI']));
    

    That gives you whatever was after the last forward slash