如何在php中获取url的一部分

如何在php中获取url的一部分,php,Php,如何获取url的最后3部分 例如,我有如下链接 http://www.yoursite/one/two/three/drink.pdf 我将使用下面的代码获取url的最后一部分 $url = "http://www.yoursite/one/two/three/drink.pdf"; echo $end = end((explode('/', $url))); $url = "http://www.yoursite/one/two/three/drink.pdf"; $url_array =

如何获取url的最后3部分

例如,我有如下链接

http://www.yoursite/one/two/three/drink.pdf
我将使用下面的代码获取url的最后一部分

$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));
但我需要从下面的url的最后3部分

/two/three/drink.pdf
请为我提供解决方案。

您可以这样做:

<?php

    $url = "http://www.yoursite/one/two/three/drink.pdf";
    $ex = explode('/', $url);

    $arr = array_slice($ex, -3, 3);
    $output = '/'.implode('/', $arr);
    var_dump($output);    // Outputs /two/three/drink.pdf
试试下面的代码

$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));

希望这有帮助。

奇怪的问题。。。如果使用explode将路径组件分开,那么为什么不能简单地使用返回的最后三个元素呢http://www.yoursite/one 在URL中保持不变,然后可以使用echo'http://www.yoursite/one/two/three/drink.pdf',23; 式中,23是管道的长度http://www.yoursite/one 你想省略的。结合