Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

PHP get the file name without the parameters

I have these urls

something2/something3/fileName.php

something2/something3/fileName.php?para=someting&param2=something2

I want to get the fileName without the .php extension and without the paramets,

note: not all the urls have parameters.

What I have tried:

$phpFileName = basename($_SERVER['REQUEST_URI'], ".php");//without .php extension

that works only when there are no parameters in the urls, but when there are parameters in the url, i got like this:

fileName.php?parm=something&param=something

how can I remove these parameters please and get just the fileName ?

thanks

Upvotes: 1

Views: 2085

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

Get the path from the URL first with parse_url:

$phpFileName = basename(parse_url($_SERVER['REQUEST_URI'], PHP_PATH), ".php");

You could also look at pathinfo.

Upvotes: 2

fire
fire

Reputation: 21531

Well if the code is in the file itself you can use...

$phpFileName = basename(__FILE__, ".php");

The __FILE__ is a magic constant to the current file being executed.

Otherwise just remove the query string using explode first...

$qs = explode("?", $_SERVER['REQUEST_URI']);
$phpFileName = basename($qs[0], ".php");

Upvotes: 2

Related Questions