How do I get a file extension in PHP?
Asked 07 September, 2021
Viewed 2.8K times
  • 58
Votes

This is a question you can read everywhere on the web with various answers:

$ext = end(explode('.', $filename));
$ext = substr(strrchr($filename, '.'), 1);
$ext = substr($filename, strrpos($filename, '.') + 1);
$ext = preg_replace('/^.*.([^.]+)$/D', '$1', $filename);

$exts = split("[/\.]", $filename);
$n    = count($exts)-1;
$ext  = $exts[$n];

etc.

However, there is always "the best way" and it should be on Stack Overflow.

29 Answer