我有一个函数可以加载它在 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);
}
最佳答案
您可以修改当前的正则表达式或使用 strstr 添加 bool 表达式(我会推荐)。
修改您当前的正则表达式:
"/^[^_].*\.(bmp|jpeg|gif|png|jpg)$/i"
或者检测字符串中下划线的简单表达式是:
strstr($file, '_')
编辑:实际上你可以使用 substr :
substr($file, 0, 1) != '_'
关于PHP正则表达式查找以_开头的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125634/