Php 提取url的最后一部分

Php 提取url的最后一部分,php,Php,需要url的最后一部分吗 那么最后一部分什么都不是 如果是,那么最后一部分就是我 如果是 最后一部分是file.php 不确定我是否使用regex或什么有一个名为的函数。这是一个简单的示例: <?php $url = "http://example.com/i/am/file.php"; $keys = parse_url($url); // parse the url $path = explode("/", $keys['path']); // spli

需要url的最后一部分吗

那么最后一部分什么都不是 如果是,那么最后一部分就是我 如果是 最后一部分是file.php


不确定我是否使用regex或什么

有一个名为的函数。

这是一个简单的示例:

 <?php
     $url = "http://example.com/i/am/file.php";
     $keys = parse_url($url); // parse the url
     $path = explode("/", $keys['path']); // splitting the path
     $last = end($path); // get the value of the last element 
 ?>

我希望这对你有帮助;)


请注意,如果出于某种原因,您有一个尾随斜杠,那么Vasil的示例将不起作用,例如一些CMS系统将放在末尾(Magento,我正在看您……)


好吧,行得通,但路的尽头是空的。需要注意的是。

对于那些使用CMS(如WordPress或Magento)添加尾部斜杠的用户,Vasil的解决方案中有一个简单的补充:

<?php
 $url = "http://example.com/i/am/file.php";
 $keys = parse_url($url); // parse the url
 $path = explode("/", $keys['path']); // splitting the path
 $last = end($path); // get the value of the last element 
 $last = prev($path); // get the next to last element 
 ?> 

您如何检索此url?通过php?它已经是一个变量了吗?可能是
<?php
 $url = "http://example.com/i/am/file.php";
 $keys = parse_url($url); // parse the url
 $path = explode("/", $keys['path']); // splitting the path
 $last = end($path); // get the value of the last element 
 $last = prev($path); // get the next to last element 
 ?> 
    $request_path = $_SERVER['REQUEST_URI'];
    $path = explode("/", $request_path); // splitting the path
    $last = end($path);
    $last = prev($path);