如何获取URL中的最后一个路径? [英] How to get the last path in a URL?

查看:526
本文介绍了如何获取URL中的最后一个路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取URL中的最后一个路径段:

I would like get the last path segment in a URL:

  • http://blabla/bla/wce/news.php
  • http://blabla/blablabla/dut2a/news.php
  • http://blabla/bla/wce/news.php or
  • http://blabla/blablabla/dut2a/news.php

例如,在这两个URL中,我要获取路径段:"wce"和"dut2a".

For example, in these two URLs, I want to get the path segment: 'wce', and 'dut2a'.

我尝试使用$_SERVER['REQUEST_URI'],但获得了完整的URL路径.

I tried to use $_SERVER['REQUEST_URI'], but I get the whole URL path.

推荐答案

尝试:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];

假设$tokens至少包含2个元素.

Assuming $tokens has at least 2 elements.

这篇关于如何获取URL中的最后一个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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