3

我有一个函数可以加载它在 wordpress 上传目录中找到的所有图像文件。我想稍微修改一下,让它跳过任何以下划线字符开头的图像,“_someimage.jpg”被跳过,而“someimage.jpg”不是......

这是现有的功能....

 $dir = 'wp-content/uploads/';
 $url = get_bloginfo('url').'/wp-content/uploads/';
 $imgs = array();
  if ($dh = opendir($dir)) 
  {
  while (($file = readdir($dh)) !== false) 
   {
   if (!is_dir($file) && preg_match("/\.(bmp|jpeg|gif|png|jpg|)$/i", $file)) 
   {
   array_push($imgs, $file);
   }
  }
  closedir($dh);
  } else {
   die('cannot open ' . $dir);
  }
4

2 回答 2

1

您可以使用strstr修改当前的正则表达式或添加布尔表达式(我推荐)。

修改您当前的正则表达式:

"/^[^_].*\.(bmp|jpeg|gif|png|jpg)$/i"

或者检测字符串中下划线的简单表达式是:

strstr($file, '_')

编辑:实际上你可以使用substr

substr($file, 0, 1) != '_'
于 2010-01-24T02:16:05.173 回答
0
if (!is_dir($file) && preg_match("/\.(bmp|jpeg|gif|png|jpg|)$/i", $file)) 

可以转化为:

if (!is_dir($file) && preg_match("/^[^_].*\.(bmp|jpeg|gif|png|jpg|)$/i", $file)) 
于 2010-01-24T02:16:47.843 回答