-1

I have some coding that was done years ago for me using eregi. I know that I need to change it to preg_match but have no idea how to do this.

the code is:

       if (eregi("\.jpg|\.gif|\.png", $f))
          array_push($files, $path.$f);

Any help would be greatly appreciated

thanks

2 Answers2

2

You should be able to use preg_match here with the i case insensitive flag:

if (preg_match("/\.(?:jpg|gif|png)/i", $f)) {
    array_push($files, $path.$f);
}

Note that the eregi function was deprecated in PHP 5.3.0 and removed in 7.0.0, see the documentation. It is time to upgrade to a current version of PHP.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

I would not use a regex to check the extension of a file. https://www.php.net/manual/en/function.pathinfo.php with in_array would be a better approach.

$path = pathinfo($f);
if(in_array(strtolower($path['extension']), array('jpg', 'gif', 'png')) {
     $files[] =  $path . $f;
}
user3783243
  • 5,368
  • 5
  • 22
  • 41