Reputation: 232
I would like to format a directory of images by removing the file extensions. I'm using basename()
to do so but I can only check for one suffix. How can I also check for .png
& .jpeg
with my code below? Is it possible to use a regex here?
function sanitize_items($value) {
$base = basename(strtolower($value), '.jpg');
$temp = explode('-', $base);
return $temp[0];
}
foreach ($list[1] as $f) {
$file_sanitized[sanitize_items($f)] = $f;
}
Upvotes: 0
Views: 111
Reputation: 13728
try pathinfo()
$ext = pathinfo($value, PATHINFO_EXTENSION);
return str_replace('.'.$ext, '', $value);
Upvotes: 0
Reputation: 5727
Make your life easy - use glob():
$pictures = glob("/your/path/*{jpg,png,gif}", GLOB_BRACE);
$result = array();
foreach ($pictures as $pic) {
$tmp = pathinfo($pic);
$result[] = $tmp['filename'];
}
var_dump($result);
Upvotes: 1