7

I'm exploding on "." to get file format and name:

list($txt, $ext) = explode(".", $name);

The problem is that some files have names with dots.

How do I explote on the LAST "." so that I get $name=pic.n2 and $ext=jpg from: pic.n2.jpg?

Unsigned
  • 9,640
  • 4
  • 43
  • 72
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

9 Answers9

18

Use pathinfo:

$pi = pathinfo($name);
$txt = $pi['filename'];
$ext = $pi['extension'];
DCoder
  • 12,962
  • 4
  • 40
  • 62
12
$name = pathinfo($file, PATHINFO_FILENAME);
$ext  = pathinfo($file, PATHINFO_EXTENSION);

http://www.php.net/pathinfo

deceze
  • 510,633
  • 85
  • 743
  • 889
6

use this

$array = explode(".", $name);
end($array);         // move the internal pointer to the end of the array
$filetype = current($array);

thanks

Saiyam Patel
  • 1,161
  • 9
  • 18
1
<?php
$path = 'http://www.mytest.com/public/images/portfolio/i-vis/abc.y1.jpg';
echo $path."<br/>";
$name = basename($path);
$dir = dirname($path); 
echo $name."<br/>";
echo $dir."<br/>";
$pi = pathinfo($path);
$txt = $pi['filename']."_trans";
$ext = $pi['extension'];
echo $dir."/".$txt.".".$ext;
?>
1

Use PHP's pathinfo() function.

See more information here http://php.net/manual/en/function.pathinfo.php

$file_part = pathinfo('123.test.php');

Example:

echo $file_part['extension'];
echo $file_part['filename'];

Output:

php 123.test

Chirag
  • 1,128
  • 8
  • 8
1

Use Pathinfo or mime_content_type to get file type information

$filetype = pathinfo($file, PATHINFO_FILENAME);

$mimetype = mime_content_type($file);
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
0

It is better to use one of the solutions above, but there is also a solution using the explode function:

$filename = "some.file.name.ext";
list($ext, $name) = explode(".", strrev($filename), 2);
$name = strrev($name);
$ext = strrev($ext);

What this solution does is the following:
1. reverse string, so it will look like: txe.eman.elif.emos
2. explode it, you will get something like: $ext = "txe", $name = "eman.elif.emos"
3. reverse each of the variables to get the correct results

Zsolti
  • 1,571
  • 1
  • 11
  • 22
0

you can write your own function as

function getExtension($str) {

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

Community
  • 1
  • 1
Sumant
  • 954
  • 1
  • 18
  • 39
0

You might try something like this:

<?php
$file = 'a.cool.picture.jpg';

$ext = substr($file, strrpos($file, '.')+1, strlen($file)-strrpos($file, '.'));
$name = substr($file, 0, strrpos($file, '.'));
echo $name.'.'.$ext;
?>

The key functions are strrpos() which finds the last occurrence of a character (a "." in this case) and substr() which returns a sub string. You find the last "." in the file, and sub string it. Hope that helps.

Jason
  • 556
  • 1
  • 5
  • 21
  • I like the explode function as well, though getting the file name requires looping or imploding, whereas this is more to the point. (Messy code as it is, granted.) – Jason Mar 07 '12 at 06:04
  • You don't have to supply the 3rd argument to `substr`, `substr($file, strrpos($file, '.')+1)` alone is fine for finding the extension. – deceze Mar 07 '12 at 06:24