如何获取不带参数的文件名?

问题描述:

我需要找到不包含GET参数的文件的文件名.

I need to find the file name of the file I've included without the GET parameters.

例如: 如果当前URL是 http://www.mysite.com/folder/file.php?a = b& c = d , 我想要file.php返回

e.g.: if the current URL is http://www.mysite.com/folder/file.php?a=b&c=d , i want file.php returned

我发现的东西:

basename($_SERVER['REQUEST_URI'])

返回:

file.php?a=b&c=d

在我的情况下: 我在购物车部分的表单中使用文件名,因此,此刻每次您单击减少产品数量"按钮时,都会在表单的末尾添加表单(productId和action)的GET参数.网址: file.php?a = b& c = d?a = b& c = d?a = b& c = d?a = b& c = d ...

in my case: I'm using the filename in a form in the section for my cart, so at the moment each time you click the 'reduce number of product' button it adds the GET params of the form (productId and action) to the end of the URL: file.php?a=b&c=d?a=b&c=d?a=b&c=d?a=b&c=d...

我知道我可以在'?'上爆炸或类似的东西

I know I could simply explode or something similar on '?'

$file = explode("?", basename($_SERVER['REQUEST_URI']))

并使用数组的第一部分,但我似乎想起来更简单,但是无法再次找到代码.

and use the first part of the array, but I seem to recall something easier, however cannot locate the code again..

我是PHP的新手,所以希望您对代码进行解释.

I'm new to PHP so explanation on your code would be appreciated.

谢谢, V

您可以使用

You can make use of parse_url. In your case you could use:

$url = parse_url($url, PHP_URL_PATH);

要仅获取文件名,您可以执行以下操作:

To get only the file name you can do something like this:

$url = explode('/', parse_url($url, PHP_URL_PATH));
$url = end($url);