I have a string: index.twig.php
and I need to get: twig.php
from it. I tried to use $ext = pathinfo($filename, PATHINFO_EXTENSION);
but it only gives me php
I need everything from the first dot.
Asked
Active
Viewed 57 times
0

durisvk
- 927
- 2
- 12
- 24
-
substr(strpos($path, ".")) – Jordan Soltman Dec 06 '16 at 18:11
-
Possible duplicate of [How to extract a file extension in PHP?](http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php) – André Dion Dec 06 '16 at 18:53
3 Answers
1
There's a function that does exactly that:
$ext = strstr($filename, '.');
You can trim the leading dot if you don't want it:
$ext = ltrim($ext, '.');

Narf
- 14,600
- 3
- 37
- 66
1
You could use join()
, explode()
and array_slice()
php functions :
i.e. :
$s = "index.twig.php";
echo join('.', array_slice(explode('.', $s), -2));
Output :
twig.php
Explanation :
expolde()
will create an array by spliting the string on every ".".
array_slice()
will slice the array to keep last two elements only.
join()
will concatenate the result by "." in a new string.
Since other answers don't take care if the filename contains additional dots (i.e. "some.file.name.twig.php"), this solution will keep last two items only to isolate the extension.

JazZ
- 4,469
- 2
- 20
- 40
0
You can use a simple explode
to do it.
$string = 'index.twig.php';
list($filename, $extension) = explode('.', $string, 2);

Federkun
- 36,084
- 8
- 78
- 90