Web Artisan

Different approaches to get file extentions by PHP

Different approaches to get file extentions by PHP

http://cowburn.info/2008/01/13/get-file-extension-comparison/
$filename = 'mypic.gif';
  1. The “explode/end” approach
    $ext = end(explode('.', $filename));
    
  2. The “strrchr” approach
    $ext = substr(strrchr($filename, '.'), 1);
    
  3. The “strrpos” approach
    $ext = substr($filename, strrpos($filename, '.') + 1);
    
  4. The “preg_replace” approach
    $ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
    
  5. The “pathinfo” approach
    $filename = 'mypic.gif';
    $ext = pathinfo($filename, PATHINFO_EXTENSION);