我有一些几年前使用eregi为我完成的编码。我知道我需要将其更改为preg_match,但不知道如何执行此操作。
代码是:
if (eregi("\.jpg|\.gif|\.png", $f))
array_push($files, $path.$f);
任何帮助将不胜感激
谢谢
您应该可以在此处将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了。
我不会使用正则表达式来检查文件的扩展名。 https://www.php.net/manual/en/function.pathinfo.php和in_array
会是更好的方法。
$path = pathinfo($f);
if(in_array(strtolower($path['extension']), array('jpg', 'gif', 'png')) {
$files[] = $path . $f;
}