My string looks like this:
$str = www.example.com/forums/pages/name.php
I will like to get this:
name
I have tried:
echo rtrim($str,".php");
But I am getting www.example.com/forums/pages/name
Thanks a lot.
My string looks like this:
$str = www.example.com/forums/pages/name.php
I will like to get this:
name
I have tried:
echo rtrim($str,".php");
But I am getting www.example.com/forums/pages/name
Thanks a lot.
You can achieve what you want by using pathinfo()
$a_info = pathinfo("www.site.com/forums/pages/name.php");
echo $a_info["filename"];
$a_info["filename"] will give you "name"
you can also get other details from pathinfo
Array
(
[dirname] => www.site.com/forums/pages
[basename] => name.php
[extension] => php
[filename] => name
)
The substr() function returns a part of a string.
Note: If the start parameter is a negative number and length is less than or equal to start, length becomes 0.
echo substr($str,26,29); // this will give output - name.php
EDIT:
$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