Php 获取部分 url 的值 [英] Php get value of part of url

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

问题描述

我有一个网址(如下).我想得到2"的值.我怎样才能得到它?

I have a url (as below). I'd like to get the value of "2". How can I get that?

http://domain.com/site1/index.php/page/2

推荐答案

您正在寻找的是 pathinfoparseurl:

What you're looking for is a combination of pathinfo and parseurl:

pathinfo(parseurl($url)['path'])['filename'];

pathinfo 会将路径分解为明确定义的部分,其中 filename 是您要查找的最后一部分 (2).如果您正在查找路径中的绝对位置,您可能希望在 / 上拆分路径并简单地获取索引 3 处的值.

pathinfo will break the path into well-defined parts, of which filename is that last part you're looking for (2). If you're looking instaed for the absolute location in the path, you may want to split the path on / and simply get the value at index 3.

我们可以这样测试:

<?php
$url = "http://domain.com/site1/index.php/page/2";
$value=pathinfo(parse_url($url)['path'])['filename'];
echo $value."\n";

然后在命令行上:

$ php url.php 
2

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

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