在URL中的last/之后获取字符 [英] Get characters after last / in url

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

问题描述

我想在http://www.vimeo.com/1234567

我如何使用php?

推荐答案

非常简单:

$id = substr($url, strrpos($url, '/') + 1);

strrpos 获取最后一次出现斜线的位置; substr 返回该位置之后的所有内容.

strrpos gets the position of the last occurrence of the slash; substr returns everything after that position.

如redanimalwar所提到的,如果没有斜杠,这将无法正常工作,因为strrpos返回false.这是一个更强大的版本:

As mentioned by redanimalwar if there is no slash this doesn't work correctly since strrpos returns false. Here's a more robust version:

$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);

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

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