[英]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.