我的 $_SERVER['REQUEST_URI']
类似于/advt/upload/siteid/1。我需要从指定路径检索“1”。我如何进行预匹配或其他操作?
谢谢!
最佳答案
您想要最后一个路径段吗?简单!
$lastPathSegment = basename($_SERVER['REQUEST_URI']);
CodePad .
您可能需要使用rtrim($_SERVER['REQUEST_URI'], '/')
来确保路径没有尾部斜杠。
还有很多其他方法可以做到这一点。假设 $uri = rtrim($_SERVER['REQUEST_URI'], '/')
...
$lastPathSegment = end(explode("/", $uri));
CodePad .
$lastPathSegment = ltrim(strrchr($uri, "/"), "/");
CodePad .
$lastPathSegment = substr($uri, 1 + strrpos($uri, "/"));
CodePad .
$lastPathSegment = preg_replace('/^.+\//', '', $uri);
CodePad .
...还有更多。
关于php - 如何使用请求url提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843279/