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;结果- index.php?c=lorem-ipsum
如何获得没有$_GET变量的文件名($_GET),如果可能的话使用array_pop?
发布于 2016-07-18 07:49:51
我将遵循以下parse_url() (容易理解):-
<?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;
?>注意:-使用您给定的URL进行测试。检查其他类型的URL在您的末端。谢谢。
发布于 2016-07-18 08:00:23
另一种可以获得文件名的方法是使用parse_url -解析一个URL并返回其组件。
<?php
$url = "http://localhost/mc/site-01-up/index.php?c=lorem-ipsum";
$data = parse_url($url);
$array = explode("/",$data['path']);
$filename = $array[count($array)-1];
var_dump($filename);结果
index.php编辑:很抱歉发布这个答案,因为它几乎与所选的答案相同。我没有看到如此发布的答案。但我不能删除这一点,因为主持人认为这是一种不好的做法。
发布于 2016-07-18 07:43:06
一种方法是简单地获取文件的basename(),然后使用regex删除所有查询部分,或者更好的做法是将$_SERVER['PHP_SELF']结果传递给basename()函数。这两种方法都会产生相同的结果,尽管第二种方法似乎更直观一些。
<?php
$fileName = preg_replace("#\?.*$#", "", basename("http://localhost/mc/site-01-up/index.php?c=lorem-ipsum"));
echo $fileName; // DISPLAYS: index.php
// OR SHORTER AND SIMPLER:
$fileName = basename($_SERVER['PHP_SELF']);
echo $fileName; // DISPLAYS: index.phphttps://stackoverflow.com/questions/38431070
复制相似问题