简体   繁体   English

如何在PHP中没有GET变量的情况下获取URL的最后一段?

[英]How to get last segment of the URL without GET variables in PHP?

Now I have it like so:现在我有这样的:

<?
  $url=$_SERVER['REQUEST_URI'];
  $requred_string= substr(strrchr($url, "/"), 1);
?>

and it returns the last segment, but it also returns all the ?p=y&g=x gibberish that I don't need.它返回最后一段,但它也返回所有我不需要的?p=y&g=x乱码。 How can I slice off the $_GET variables?如何切掉$_GET变量?

You can use strtok to exclude the query strings:您可以使用strtok排除查询字符串:

$url = strtok($_SERVER['REQUEST_URI'], '?');
$requred_string = substr(strrchr($url, '/'), 1);
echo $requred_string;

Or as mentioned in the comments, parse_url will work also:或者如评论中所述, parse_url也将起作用:

$requred_string = substr(strrchr(parse_url($url)['path'], '/'), 1); // PHP 5.4 or greater with dereference

这是一种更优雅的方式basename(parse_url($url, PHP_URL_PATH))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM