bonaca bonaca - 4 months ago 11
PHP Question

How to get file name from url without $_GET variable?

http://localhost/mc/site-01-up/index.php?c=lorem-ipsum

$address = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$stack = explode('/', $_SERVER["REQUEST_URI"]);
$file = array_pop($stack);
echo $file;


result -
index.php?c=lorem-ipsum


How to get just file name (
index.php
) without
$_GET
variable, using
array_pop
if possible?

Answer

I will follow parse_url() like below (easy to understand):-

<?php
$url = 'http://localhost/mc/site-01-up/index.php?c=lorem-ipsum';

$url=   parse_url($url);
print_r($url); // to check what parse_url () will outputs
$url_path = explode('/',$url['path']); // explode the path part
$file_name =  $url_path[count($url_path)-1]; // get last index value which is your desired result

echo $file_name;
?>

Output:- https://eval.in/606839

Note:- tested with your given URL. Check for other type of URL's at your end. thanks.

Comments