Friday, 3 June 2016

PHP- GETTING THE FILENAME FROM A URL

Someone emailed me, asking me how to get the filename from a URL using PHP. It’s pretty easy, but I thought i’d jot it down for everyone, just in case other people want the same result.

Example

Ok, so we want the filename for the following page:
http://www.anydomain.co.uk/page/grab.php
We want to grab the filename, which is grab.php
The following code will do that for you:
1
$basename = basename($_SERVER['REQUEST_URI']);
If you want to remove the .php, a simple string replace function will do:
1
$basename = str_replace(".php","",basename($_SERVER['REQUEST_URI']));

0 comments:

Post a Comment