I want to get filename without any $_GET variable values from a url in php?
My url is http://learner.com/learningphp.php?lid=1348
I only want to retrieve the learningphp.php
from the url?
How to do this? Please help.
I used basename but it gives all the variable values also- learntolearn.php?lid=1348
which are in the url.
This should work:
But beware of any malicious parts in your URL.
Use
parse_url()
as Pekka said:http://codepad.org/NBBf4yTB
In this example the optional username and password aren't output!
Try the following code:
For PHP 5.4.0 and above:
For PHP Version < 5.4.0
You can use,
Is better to use
parse_url
to retrieve only the path, and then getting only the filename with thebasename
. This way we also avoid query parameters.Is somewhat similar to Sultan answer excepting that I'm using
component
parse_url
parameter, to obtain only the path.An other way to get only the filename without querystring is by using parse_url and basename functions :