The "best" way depends on the context and what you are doing with that file extension.
However,
pathinfo in general is the best when you consider all the angles.
pathinfo($file, PATHINFO_EXTENSION)
It is not the fastest, but it is fast enough. It is easy to read, easy to remember and reuse everywhere. Anyone can understand it at a glance and remove PATHINFO_EXT flag if they need more info about the file.
❌ strrpos method. described in several answers is faster yes but requires additional safety checks which, in turn, requires you to wrap it inside a function, to make it easily reusable.
Then you must take the function with you from project to project or look it up.
Wrapping it in a function call with extra checks also makes it slower and if you need any other info about the file you now have other methods to call and at that point, you lose the speed advantage anyway whilst having a solution that's harder to read.
The potential for speed is there but is not worth it unless you need to address such a bottleneck.
❌ I'd also rule out any ideas using substr, explode, and most other manual manipulations for the same reasons mentioned above.
❌SplFileInfo is very cool but takes up much more brain space with a lot of interfaces that you no doubt waste time learning only to look them up again next time. I'd only use it in specific cases where you will find the extra interfaces worth someone learning Spl when they come back to add/edit your code later.
❌ I would not consider preg_replace at all as any regex function in PHP is on average 3 times slower than any other function, is harder to read, and is in most cases can easily be done with something simpler. Regex is powerful and it has its place in those specific situations where it can replace several method calls and condition checks in one line.
Getting a file extension this way is like using an anvil to hammer in a nail.
While of course "the best" would come down to public opinion, I'd argue that other methods are only "the best" in specialized cases.
For example, if you just want to check for a specific type then I wouldn't use any of the suggested methods as stripos would be the fastest case insensitive comparison to use.
if (stripos('/here/is/sOme.fiLe.PdF', '.pdf', -4) !== false )
{
//its a pdf file
}
But again pathinfo would still be nicer to read and probably worth the performance cost.
But what about https://ome.Com.///lica.ted?URLS ?
Extracting paths from URLs is a separate concern that is outside the scope of the question and will require an extra step in any case where a simple one-time string comparison won't do.