php - 如何使用请求url提取值

标签 php html

我的 $_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/

相关文章:

php - 带 REDIS 的套接字 IO。服务器收不到 channel 消息

php - 在 php $query 中选择 mysql 数据库的表

php - 使用 PDO 准备语句插入密码哈希

javascript - 我想使用 Jquery 向我的 html 元素添加更多 css 属性,这意味着我不想删除前一个属性,只需添加更多属性即可

javascript - Node.JS、ExpressJS、React 和 HTML JSON 渲染

php - 底部的导航链接不显示在其他页面上,只显示在第一个

php - 圆形图像?

html - 无法在子列表中删除直通线

html - Angular:仅组件高度到屏幕

html - 更改 primefaces 选择列表的宽度