Well you could use something better to get the extension like this
$ext = strrchr($file, ".");
The only difference is this keeps the .
so if $file
is somefile.txt
it return .txt
which if you want to get rid of you could always do this
$ext = ltrim(strrchr($file, "."), '.');
For reference
http://php.net/manual/en/function.strrchr.php
strrchr — Find the last occurrence of a character in a string.
string strrchr ( string $haystack , mixed $needle )
This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.
So it just finds the last .
and return that and everything after, then ltrim
is just "left" trim.
P.S. I really dislike using explode for getting the extension, it's one of my pet peeves.
So for your particular case I would:
foreach($filelist as $value => $file) {
$ext = ltrim(strrchr($file, "."), '.');
if(in_array($ext, $extensions) && is_file($dir.$file)) { $c++; }
if(isset($_GET['start'])) { $nav = $_GET['start']; } else { $nav = "0"; }
if(($c > $nav)&&($c < $nav+($pagerows+1))) {
$link = $dir . $file;
$hlink = $http . $file;
//$ext = explode(".", $file); we've already done this no need to do it again
This way, you get the extension one time, you don't create an array for it explode
, you don't move the array pointer to the end of the array end
and that's about it.
UPDATE
You can compare these two using microtime
to see which is faster, they are both really fast so we have to do about 100k iterations to test it, like this:
$filename = "filename.php";
$start = microtime(true);
for($i=0; $i<=100000; $i++){
$v=explode(".", $filename);
$ext1 = end($v);
}
echo "1. ext='$ext1' Complete ".number_format((microtime(true) - $start), 4).PHP_EOL;
$start = microtime(true);
for($i=0; $i<=100000; $i++){
$ext2 = ltrim(strrchr($filename, '.'), '.');
}
echo "2. ext='$ext2' Complete ".number_format((microtime(true) - $start), 4).PHP_EOL;
Outputs
1. ext='php' Complete 0.0178
2. ext='php' Complete 0.0098
----------------------------
1. ext='php' Complete 0.0237
2. ext='php' Complete 0.0125
---------------------------
1. ext='php' Complete 0.0252
2. ext='php' Complete 0.0098
---------------------------
1. ext='php' Complete 0.0190
2. ext='php' Complete 0.0102
You can test it here online
One thing is clear, it's almost 2x faster, not that it matters much in this case. But, it never hurts to check these things. Both give equivalent results, but strrchr
is easier to read, well if you know what the function does anyway. It's kind of an obscure function but it basically means string right character.
Cheers.