How to remove all images from an array with a specific filename
I am using the code below to crea开发者_运维问答te an array of images. I'd love to be able to not add any images with -c.jpg
in the filename. How can I do this?
<?php
$jsarray = array();
$iterator = new DirectoryIterator(dirname("public/images/portfolio/all/"));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
//filtering to exclude the color images
$jsarray[] = "'" . $fileinfo->getFilename() . "'";
}
}
$jsstring = implode(",", $jsarray);
?>
I'm using PHP5.
$jsarray = array();
$iterator = new DirectoryIterator(dirname("public/images/portfolio/all/"));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile() && !preg_match('/-c\.jpg$/', $fileinfo->getFilename())) {
$jsarray[] = "'" . $fileinfo->getFilename() . "'";
}
}
$jsstring = implode(",", $jsarray);
That’s it.
if(strpos($fileinfo->getFilename(), "-c.jpg") === false) {
$jsarray[] = "'" . $fileinfo->getFilename() . "'";
}
Try that. strpos tells you the position of the search string if it's there, and false
if it isn't.
精彩评论