在 PHP 中的斜杠后从 URL 中获取最后一个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5921075/
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 last word from URL after a slash in PHP
提问by DiegoP.
I need to get the very last word from an URL. So for example I have the following URL:
我需要从 URL 中获取最后一个词。例如,我有以下 URL:
http://www.mydomainname.com/m/groups/view/test
http://www.mydomainname.com/m/groups/view/test
I need to get with PHP only "test", nothing else. I tried to use something like this:
我只需要使用 PHP 进行“测试”,别无其他。我试图使用这样的东西:
$words = explode(' ', $_SERVER['REQUEST_URI']);
$showword = trim($words[count($words) - 1], '/');
echo $showword;
It does not work for me. Can you help me please?
它对我不起作用。你能帮我吗?
Thank you so much!!
非常感谢!!
回答by sanjary
回答by fardjad
by using regex:
通过使用正则表达式:
preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test
回答by Wouter den Ouden
I used this:
我用过这个:
$lastWord = substr($url, strrpos($url, '/') + 1);
Thnx to: https://stackoverflow.com/a/1361752/4189000
感谢:https://stackoverflow.com/a/1361752/4189000
回答by Gumbo
You can use explode
but you need to use /
as delimiter:
您可以使用,explode
但需要/
用作分隔符:
$segments = explode('/', $_SERVER['REQUEST_URI']);
Note that $_SERVER['REQUEST_URI']
can contain the query string if the current URI has one. In that case you should use parse_url
before to only get the path:
请注意,$_SERVER['REQUEST_URI']
如果当前 URI 具有查询字符串,则可以包含查询字符串。在这种情况下,您应该使用parse_url
before 只获取路径:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
And to take trailing slashes into account, you can use rtrim
to remove them before splitting it into its segments using explode
. So:
并考虑到尾部斜杠,您可以使用rtrim
删除它们,然后使用explode
. 所以:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', rtrim($_SERVER['REQUEST_URI_PATH'], '/'));
回答by Robik
To do that you can use explode
on your REQUEST_URI
.I've made some simple function:
为此,您可以explode
在您的REQUEST_URI
.I 上使用一些简单的功能:
function getLast()
{
$requestUri = $_SERVER['REQUEST_URI'];
# Remove query string
$requestUri = trim(strstr($requestUri, '?', true), '/');
# Note that delimeter is '/'
$arr = explode('/', $requestUri);
$count = count($arr);
return $arr[$count - 1];
}
echo getLast();
回答by publikz.com
use preg*
使用预浸*
if ( preg_match( "~/(.*?)$~msi", $_SERVER[ "REQUEST_URI" ], $vv ))
echo $vv[1];
else
echo "Nothing here";
this was just idea of code. It can be rewriten in function.
这只是代码的想法。它可以在函数中重写。
PS. Generally i use mod_rewriteto handle this... ans process in php the $_GET variables. And this is good practice, IMHO
附注。通常我使用mod_rewrite来处理这个... ans 进程在 php 中的 $_GET 变量。这是一个很好的做法,恕我直言
回答by Dan Bray
If you don't mind a query string being included when present, then just use basename
. You don't need to use parse_url
as well.
如果您不介意在存在时包含查询字符串,则只需使用basename
. 你也不需要使用parse_url
。
$url = 'http://www.mydomainname.com/m/groups/view/test';
$showword = basename($url);
echo htmlspecialchars($showword);
When the $url
variable is generated from user input or from $_SERVER['REQUEST_URI']
; before using echo
use htmlspecialchars
or htmlentities
, otherwise users could add html
tags or run JavaScript
on the webpage.
当$url
变量是从用户输入或从$_SERVER['REQUEST_URI']
; 在echo
使用htmlspecialchars
或之前htmlentities
,否则用户可以添加html
标签或JavaScript
在网页上运行。
回答by Vedurupaka Mahesh
ex: $url = 'http://www.youtube.com/embed/ADU0QnQ4eDs';
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
// **output**: $basename is "ADU0QnQ4eDs"
complete solution you will get in the below link. i just found to Get last word from URL after a slash in PHP.
您将在以下链接中获得完整的解决方案。我刚刚发现在 PHP 中的斜线后从 URL 获取最后一个词。