我如何用preg_match替换eregi函数?

问题描述 投票:0回答:2

我有一些几年前使用eregi为我完成的编码。我知道我需要将其更改为preg_match,但不知道如何执行此操作。

代码是:

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

任何帮助将不胜感激

谢谢

php preg-match eregi
2个回答
1
投票

您应该可以在此处将preg_match与不区分大小写的i标志一起使用:

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

请注意,eregi函数在PHP 5.3.0中已弃用,而在7.0.0中已删除,请参见documentation。现在是时候升级到最新版本的PHP了。


0
投票

我不会使用正则表达式来检查文件的扩展名。 https://www.php.net/manual/en/function.pathinfo.phpin_array会是更好的方法。

$path = pathinfo($f);
if(in_array(strtolower($path['extension']), array('jpg', 'gif', 'png')) {
     $files[] =  $path . $f;
}
© www.soinside.com 2019 - 2024. All rights reserved.