php 仅从php中的url获取文件名,url中不存在任何变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7852296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get only filename from url in php without any variable values which exist in the url
提问by sqlchild
I want to get filename without any $_GET
variable values from a URL in php?
我想$_GET
从 php 中的 URL获取没有任何变量值的文件名?
My URL is http://learner.com/learningphp.php?lid=1348
我的网址是 http://learner.com/learningphp.php?lid=1348
I only want to retrieve the learningphp.php
from the URL?
我只想learningphp.php
从 URL 中检索 ?
How to do this?
这该怎么做?
I used basename()
function but it gives all the variable values also: learntolearn.php?lid=1348
which are in the URL.
我使用了basename()
函数,但它也提供了所有变量值:learntolearn.php?lid=1348
它们在 URL 中。
回答by str
This should work:
这应该有效:
echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);
But beware of any malicious parts in your URL.
但要注意 URL 中的任何恶意部分。
回答by Khadka Pushpendra
Following steps shows total information about how to get file, file with extension, file without extension. This technique is very helpful for me. Hope it will be helpful to you too.
以下步骤显示了有关如何获取文件、带扩展名的文件、不带扩展名的文件的总信息。这个技巧对我很有帮助。希望对你也有帮助。
$url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
$file = file_get_contents($url); // to get file
$name = basename($url); // to get file name
$ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
$name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
回答by ComFreek
Use parse_url()
as Pekka said:
parse_url()
像 Pekka 所说的那样使用:
<?php
$url = 'http://www.example.com/search.php?arg1=arg2';
$parts = parse_url($url);
$str = $parts['scheme'].'://'.$parts['host'].$parts['path'];
echo $str;
?>
In this example the optional username and password aren't output!
在此示例中,不输出可选的用户名和密码!
回答by foxtrot
Is better to use parse_url
to retrieve only the path, and then getting only the filename with the basename
. This way we also avoid query parameters.
最好parse_url
仅用于检索路径,然后仅获取带有basename
. 这样我们也避免了查询参数。
<?php
// url to inspect
$url = 'http://www.example.com/image.jpg?q=6574&t=987';
// parsed path
$path = parse_url($url, PHP_URL_PATH);
// extracted basename
echo basename($path);
?>
Is somewhat similar to Sultan answerexcepting that I'm using component
parse_url
parameter, to obtain only the path.
有点类似于Sultan 的回答,只是我使用的是component
parse_url
参数,只获取路径。
回答by SwR
You can use,
您可以使用,
$directoryURI =basename($_SERVER['SCRIPT_NAME']);
echo $directoryURI;
回答by ??????
An other way to get only the filename without querystring is by using parse_url and basename functions :
另一种只获取没有查询字符串的文件名的方法是使用 parse_url 和 basename 函数:
$parts = parse_url("http://example.com/foo/bar/baz/file.php?a=b&c=d");
$filename = basename($parts["path"]); // this will return 'file.php'
回答by Sultan
Try the following code:
试试下面的代码:
For PHP 5.4.0 and above:
对于 PHP 5.4.0 及更高版本:
$filename = basename(parse_url('http://learner.com/learningphp.php?lid=1348')['path']);
For PHP Version < 5.4.0
对于 PHP 版本 < 5.4.0
$parsed = parse_url('http://learner.com/learningphp.php?lid=1348');
$filename = basename($parsed['path']);
回答by Udhav Sarvaiya
Your URL:
您的网址:
$url = 'http://learner.com/learningphp.php?lid=1348';
$file_name = basename(parse_url($url, PHP_URL_PATH));
echo $file_name;
output:learningphp.php
输出:learningphp.php
回答by James
$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
Use parse_url to extract the path from the URL, then pathinfo returns the filename from the path
使用 parse_url 从 URL 中提取路径,然后 pathinfo 从路径中返回文件名
回答by Mob
$url = "learner.com/learningphp.php?lid=1348";
$l = parse_url($url);
print_r(stristr($l['path'], "/"));