
File Extension Using PHP
Getting the file extension is not a difficult task. But there can 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 only recently I came across an amusing delirium with a thick regular expression and all in order to just get a few characters at the end of the line after the last point. What for? So, let's start with the five most common methods. Without the use of 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 oneThe logic is as follows: using the explode () function , the resulting string is converted to an array of strings, the boundaries of which in the original were separated by a “dot”. And everything would be fine if we are talking about a file name in the style of "file.txt", but what if there are several points? To do this, end () returns the last element of the array, i.e. what happened after the last point.
Second way
Third wayIn this case, strrpos () returns the position of the last point in the line, and substr () cuts out all the characters, starting from the position of the point obtained earlier, 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).
The fourth wayIt works as follows: strrchr () returns the portion of the string following the specified parameter (the dot in our case), after which substr () cuts off the first character - the dot.
Fifth wayThis method is very similar to the first. array_pop () - pushes the element at the end of the array, end () - sets the internal pointer of the array to the last element.
What is faster? Yes, in other matters, in practice, the results of execution and all methods are approximately the same. To confirm my guesses, I conducted a small test, having run each option 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.
Do you think that the question is trivial and everything is just like 2 fingers? Maybe only recently I came across an amusing delirium with a thick regular expression and all in order to just get a few characters at the end of the line after the last point. What for? So, let's start with the five most common methods. Without the use of 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 oneThe logic is as follows: using the explode () function , the resulting string is converted to an array of strings, the boundaries of which in the original were separated by a “dot”. And everything would be fine if we are talking about a file name in the style of "file.txt", but what if there are several points? To do this, end () returns the last element of the array, i.e. what happened after the last point.
Second way
- function getExtension2 ($ filename) {
- $ path_info = pathinfo ($ filename);
- return $ path_info ['extension'];
- }
Third wayIn this case, strrpos () returns the position of the last point in the line, and substr () cuts out all the characters, starting from the position of the point obtained earlier, 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).
The fourth wayIt works as follows: strrchr () returns the portion of the string following the specified parameter (the dot in our case), after which substr () cuts off the first character - the dot.
Fifth wayThis method is very similar to the first. array_pop () - pushes the element at the end of the array, end () - sets the internal pointer of the array to the last element.
What is faster? Yes, in other matters, in practice, the results of execution and all methods are approximately the same. To confirm my guesses, I conducted a small test, having run each option 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.