Tuesday, August 6, 2013

My Javascript and PHP Way to Extract the File Extension

Javascript:

var extension;
var filename = "test.my.txt";
var ext = filename.split(".");
if (ext[1] != "") {
     extension = ext[ext.length-1];
}

// extension will contain "txt"


PHP:

$fullfilename = "test.my.txt";
preg_match("/(.+)\.([^\.]+)$/",$fullfilename,$match);
$filename = $match[1];
$extension = $match[2]; // $extension will contain "txt"



I think the codes are very self-explanatory. Enjoy!!!

No comments:

Post a Comment