1

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

1
  • pathinfo() -> ["filename"] Commented Dec 29, 2014 at 16:26

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.