in array the image extension get variables

Supposing you have array of file names:

$files = array('file1.jpg', 'file2.PNG', 'file3.txt');

You might want to filter images only like this:

$extensions = array('jpg', 'jpeg', 'png');
$images = array_filter($files, function($file) use ($extensions) {
     $pos = strrpos($file, '.');
     $extension = strtolower(substr($file, $pos + 1));
     return in_array($extension, $extensions);
});

(According to posts on php.net, pathinfo is a bit slower than raw substr operations.)


Testing:

print_r($images);

Outputs:

Array
(
    [0] => file1.jpg
    [1] => file2.PNG
)

Browse More Popular Posts

Leave a Comment