在PHP中使用斜杠后,从URL获取最后一个单词

时间:2022-10-20 09:59:47

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

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!!

非常感谢!!

8 个解决方案

#1


19  

by using regex:

使用正则表达式:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test

#2


34  

Use basename with parse_url:

将basename与parse_url一起使用:

echo basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

#3


3  

I used this:

我用过这个:

$lastWord = substr($url, strrpos($url, '/') + 1);

Thnx to: https://*.com/a/1361752/4189000

Thnx:https://*.com/a/1361752/4189000

#4


2  

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:

请注意,如果当前URI具有查询字符串,则$ _SERVER ['REQUEST_URI']可以包含查询字符串。在这种情况下,您应该使用之前的parse_url来获取路径:

$_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'], '/'));

#5


1  

To do that you can use explode on your REQUEST_URI.I've made some simple function:

要做到这一点,你可以使用REQUEST_URI上的爆炸。我已经做了一些简单的功能:

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();

#6


0  

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_rewrite to handle this... ans process in php the $_GET variables. And this is good practice, IMHO

PS。通常我使用mod_rewrite来处理这个...在PHP中的$ _GET变量进程。这是一个很好的做法,恕我直言

#7


0  

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.

从用户输入或$ _SERVER ['REQUEST_URI']生成$ url变量时;在使用echo之前使用htmlspecialchars或htmlentities,否则用户可以在网页上添加html标签或运行JavaScript。

#8


-3  

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获取最后一个单词。

Get last parameter of url in php

在php中获取url的最后一个参数

#1


19  

by using regex:

使用正则表达式:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test

#2


34  

Use basename with parse_url:

将basename与parse_url一起使用:

echo basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

#3


3  

I used this:

我用过这个:

$lastWord = substr($url, strrpos($url, '/') + 1);

Thnx to: https://*.com/a/1361752/4189000

Thnx:https://*.com/a/1361752/4189000

#4


2  

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:

请注意,如果当前URI具有查询字符串,则$ _SERVER ['REQUEST_URI']可以包含查询字符串。在这种情况下,您应该使用之前的parse_url来获取路径:

$_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'], '/'));

#5


1  

To do that you can use explode on your REQUEST_URI.I've made some simple function:

要做到这一点,你可以使用REQUEST_URI上的爆炸。我已经做了一些简单的功能:

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();

#6


0  

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_rewrite to handle this... ans process in php the $_GET variables. And this is good practice, IMHO

PS。通常我使用mod_rewrite来处理这个...在PHP中的$ _GET变量进程。这是一个很好的做法,恕我直言

#7


0  

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.

从用户输入或$ _SERVER ['REQUEST_URI']生成$ url变量时;在使用echo之前使用htmlspecialchars或htmlentities,否则用户可以在网页上添加html标签或运行JavaScript。

#8


-3  

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获取最后一个单词。

Get last parameter of url in php

在php中获取url的最后一个参数