Get file extension


/ Published in: PHP
Save to your folder(s)

This function works correct with files with no extension.


Copy this code and paste it in your HTML
  1. function getExtension($filename)
  2. {
  3. return ('.' . ($ext = pathinfo($filename, PATHINFO_EXTENSION)) == $filename) ? null : $ext;
  4. }
  5.  
  6. // Example:
  7. var_dump(getExtension('.htaccess'), getExtension('image.jpg'));
  8. // Result:
  9. // NULL
  10. // string(3) "jpg"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.