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 //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.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we will learn how to get the current file extensions in PHP.
Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css"
Using $_SERVER[‘SCRIPT_NAME’]:
$_SERVER is an array of stored information such as headers, paths, and script locations. These entries are created by the webserver. There is no other way that every web server will provide any of this information.
Syntax:
$_SERVER[‘SCRIPT_NAME’]
- ‘SCRIPT_NAME’ gives the path from the root to include the name of the directory.
Method 1: The following method uses the strpos[]and substr[] methodsto print the values of the last occurrences.
PHP
Method 2: The following method uses a predefined function pathinfo[]. In the output, the “Name:” shows the name of the file and “Extension:” shows the file extension.
PHP code:
PHP
Output
Name: 001510d47316b41e63f337e33f4aaea4 Extension: php
Method 3: The following code uses thepredefined function parse_url[]and pathinfo[] for URLs.
PHP code:
PHP
Output
Name: file.index Extension: php