Getting the file extension is not the most difficult task. But there may be several approaches to its solution. Let's try to consider several possible options and understand the principles of their work.
Do you think that the question is trivial and everything is just like 2 fingers? Maybe it was only recently that I met an amusing break-line with a thick regular expression and everything just to get a few characters at the end of the line after the last dot. What for? So let's start with the five most common ways. Without using regular expressions (just don’t think that I consider them evil). I will write as simple and accessible, this is not a scientific article, but an author's note.
Method one- function getExtension1 ($ filename) {
- return end ( explode (".", $ filename));
- }
The logic is as follows: using the
explode () function, the resulting string is converted into an array of strings whose boundaries in the original were separated by a “dot”. And everything would be fine if we are talking about the file name in the “file.txt” style, but what if there are several points? For this
end () returns the last element of the array, i.e. what was after the last point.
')
Second way- function getExtension2 ($ filename) {
- $ path_info = pathinfo ($ filename);
- return $ path_info ['extension'];
- }
This is where the
pathinfo () function comes in, which returns an associative array containing information about the file we need. And if your task is to find out not only the file extension, as well as the full path to it and the full file name, then this method is for you: the array returned by this function contains the elements
dirname ,
basename and
extension - all the necessary information is in them.
Third way- function getExtension3 ($ filename) {
- return substr ($ fileName, strrpos ($ fileName, '.') + 1);
- }
In this case,
strrpos () returns the position of the last point in the line, and
substr () cuts out all the characters, starting from the previously obtained position of the point, to the end of the line. To get rid of the point itself in the resulting substring, we increase the start of the start by one shift to the right (+1).
Fourth method- function getExtension4 ($ filename) {
- return substr ( strrchr ($ fileName, '.'), 1);
- }
It works as follows:
strrchr () returns the portion of the line following the specified parameter (a dot in our case), after which
substr () cuts the first character - a dot.
Method Five- function getExtension5 ($ filename) {
- return array_pop ( explode (".", $ filename));
- }
This method is very similar to the first.
array_pop () - pushes the element at the end of the array,
end () - sets the internal array pointer to the last element.
What is faster? Yes, in other, in practice, the results of implementation and all methods are about the same. To confirm his guesses, he conducted a small test, driving each of the variants 50,000 times in a cycle:
Method # 1: 0.6777439 sec.
Method # 2: 0.5664740 sec.
Method # 3: 0.6604638 sec.
Method # 4: 0.4782789 sec.Method # 5: 0.6564250 sec.
Which one to use? You decide.