This is a question you can read everywhere on the web with various answers:
solution 1
People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP.
In fact, it does exist, but few people know it. Meet pathinfo()
:
$ext = pathinfo($filename, PATHINFO_EXTENSION);
This is fast and built-in. pathinfo()
can give you other information, such as canonical path, depending on the constant you pass to it.
If you want to be able to deal with non-ASCII characters, you need to set the locale first. E.G:
setlocale(LC_ALL,'en_US.UTF-8');
Note: This doesn’t take into consideration the file content or mime type, you only get the extension. But it’s what you asked for.