Stereo99
Stereo99

Reputation: 232

Checking for multiple image extensions?

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

Answers (3)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try pathinfo()

$ext = pathinfo($value, PATHINFO_EXTENSION);
return str_replace('.'.$ext, '', $value);

Upvotes: 0

Hannes Schneidermayer
Hannes Schneidermayer

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

Jon
Jon

Reputation: 437366

Use pathinfo instead:

$temp = explode('-', pathinfo($value, PATHINFO_FILENAME));
return strtolower($temp[0]);

Upvotes: 1

Related Questions