我的字符串如下:
$str = www.example.com/forums/pages/name.php
我想得到这个:
name
我尝试过:
echo rtrim($str,".php");
但是我得到www.example.com/forums/pages/name
非常感谢。
答案 0 :(得分:5)
您可以使用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
)
答案 1 :(得分:-1)
substr()函数返回字符串的一部分。
注意:如果start参数为负数且长度小于或等于start,则长度变为0。
echo substr($str,26,29); // this will give output - name.php
编辑:
$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