更新时间:2023-12-04 13:35:40
您可以使用PHP的 parse_url( )
功能分割的网址给你,然后访问路径
参数,并得到它的结束:
You can use PHP's parse_url()
function to split the URL for you and then access the path
parameter and get the end of it:
$r = parse_url($url);
$endofurl = substr($r['path'], strrpos($r['path'], '/'));
这将解析URL,然后采取URL的串子,从开始的最后一个发现的 /
路径。
This will parse the URL and then take a "sub-string" of the URL starting from the last-found /
in the path.
您可以选择使用爆炸('/')
作为目前工作的道路上:
You can alternatively use explode('/')
as you're currently doing on the path:
$path = explode($r['path']);
$endofurl = $path[count($path) - 1];
更新(使用 strrchr()
,指出了@ x4rf41):结果
获得字符串的结尾,而不是 SUBSTR()
+ strrpos()
是使用更短的方法 strrchr()
:
UPDATE (using strrchr()
, pointed out by @x4rf41):
A shorter method of obtaining the end of the string, opposed to substr()
+ strrpos()
is to use strrchr()
:
$endofurl = strrchr($r['path'], '/');
如果您利用 parse_url的()
的选项参数,你还可以得到的只是路径的使用 PHP_URL_PATH
像
$ R = parse_url($网址,PHP_URL_PATH);
If you take advantage of parse_url()
's option parameters, you can also get just the path by using PHP_URL_PATH
like
$r = parse_url($url, PHP_URL_PATH);
或者,最短的方法:
$endofurl = strrchr(parse_url($url, PHP_URL_PATH), '/');