在 PHP 中获取部分 URL [英] Get parts of URL in PHP

查看:38
本文介绍了在 PHP 中获取部分 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道使用 PHP 从 URL 中提取参数"的最佳方法是什么.

I am just wondering what the best way of extracting "parameters" from an URL would be, using PHP.

如果我得到了网址:

http://example.com/user/100

如何使用 PHP 获取用户 ID (100)?

How can I get the user id (100) using PHP?

推荐答案

要彻底,您需要从 parse_url().

$parts=parse_url("http://example.com/user/100");

这将为您提供一个包含少量键的数组.您正在寻找的是 path.

That will give you an array with a handful of keys. The one you are looking for is path.

分割 / 上的路径并取最后一个.

Split the path on / and take the last one.

$path_parts=explode('/', $parts['path']);

您的 ID 现在位于 $path_parts[count($path_parts)-1].

Your ID is now in $path_parts[count($path_parts)-1].

这篇关于在 PHP 中获取部分 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆