Php 从没有路径或扩展名的URL获取文件名?

Php 从没有路径或扩展名的URL获取文件名?,php,Php,我的字符串如下所示: $str = www.example.com/forums/pages/name.php 我想得到这个: name 我试过: echo rtrim($str,".php"); 但是我得到了www.example.com/forums/pages/name 非常感谢。您可以使用pathinfo实现您想要的功能 $a_info = pathinfo("www.site.com/forums/pages/name.php"); echo $a_info["filename"

我的字符串如下所示:

$str = www.example.com/forums/pages/name.php
我想得到这个:

name
我试过:

echo rtrim($str,".php");
但是我得到了www.example.com/forums/pages/name


非常感谢。

您可以使用pathinfo实现您想要的功能

$a_info = pathinfo("www.site.com/forums/pages/name.php");
echo $a_info["filename"];
$a_info[filename]将显示您的姓名

您还可以从pathinfo获取其他详细信息

Array
(
    [dirname] => www.site.com/forums/pages
    [basename] => name.php
    [extension] => php
    [filename] => name
)

substr函数返回字符串的一部分

注意:如果开始参数为负数,且长度小于或等于开始,则长度变为0

编辑:

$str = "www.example.com/forums/pages/name.php";
$start = strrpos($str,"/")+1; // using strrpos to get position of "/"
$length = strrpos($str,".")-$start; // using strrpos to get position of "." and then subtracting start position to get its length
echo substr($str,$start,$length); // this will echo out the name

我想从字符串中删除最后一个字符,清除它前面的字符,然后返回其余的。->您将得到一个空字符串:请您重新表述一下,并分享您的尝试好吗?这可能是一个非常不稳定的答案。是的,我同意这一点,在这种情况下,您应该删除或改进此答案。@user3783243检查ans的更新。现在正确吗?更好。我还是更喜欢这个内置函数。以这种方式进行分析仍然会产生不正确的结果。e、 g.my.file.jpg、.htaccess
$str = "www.example.com/forums/pages/name.php";
$start = strrpos($str,"/")+1; // using strrpos to get position of "/"
$length = strrpos($str,".")-$start; // using strrpos to get position of "." and then subtracting start position to get its length
echo substr($str,$start,$length); // this will echo out the name