$str = \’filename.mp3\’;
$ext = strrchr($str,\’.\’);
the difference is that the extension is returned with the dot in the head but it is as easy to check against \’.mp3\’ as against \’mp3\’ or any other file format.
do you consider file name like script.inc.php
or 4 letter extensions like .jpeg or .mpeg
php has a ready solution : the pathinfo() function (since v. 4.03)
even if you use explode you must do something like :
$fname = \’script.inc.php\’;
$exts = explode(\’.\’, $fname);
print $exts[sizeof($exts)-1];
July 23rd, 2002 at 11:11 am
$str = \’filename.mp3\’;
$ext = strrchr($str,\’.\’);
the difference is that the extension is returned with the dot in the head but it is as easy to check against \’.mp3\’ as against \’mp3\’ or any other file format.
September 24th, 2002 at 9:49 pm
a combination of the two gets the extension without the dot:
$ext = substr(strrchr($filename, \’.\’), 1);
September 29th, 2002 at 5:44 pm
$ext=end(explode(\’.\’,$filename));
September 29th, 2002 at 5:58 pm
definitely shorter – nice one 😉
December 21st, 2002 at 7:47 am
do you consider file name like script.inc.php
or 4 letter extensions like .jpeg or .mpeg
php has a ready solution : the pathinfo() function (since v. 4.03)
even if you use explode you must do something like :
$fname = \’script.inc.php\’;
$exts = explode(\’.\’, $fname);
print $exts[sizeof($exts)-1];
December 21st, 2002 at 3:22 pm
check out the suggestion from September 29 2002, an even shorter, more elegant way 😉
December 12th, 2007 at 10:50 am
nice one short and simple