I'm trying to figure out a way to detect groups of files. For instance:
If a given directory has the following files:
I would like to condense the listing to something like
How should I go about detecting the groups?
Here's one way you can solve this, which is more efficient than a brute force method.
preg_replace('/\d//g', $key)
).You will have something like $arr1 = [Birthday001 => Birthday, Birthday002 => Birthday ...]
$arr2 = [Birthday => 2, ...]
Simply build a histogram whose keys are modified by a regex:
<?php
# input
$filenames = array("Birthday001.jpg", "Birthday002.jpg", "Birthday003.jpg", "Picknic1.jpg", "Picknic2.jpg", "Afternoon.jpg");
# create histogram
$histogram = array();
foreach ($filenames as $filename) {
$name = preg_replace('/\d+\.[^.]*$/', '', $filename);
if (isset($histogram[$name])) {
$histogram[$name]++;
} else {
$histogram[$name] = 1;
}
}
# output
foreach ($histogram as $name => $count) {
if ($count == 1) {
echo "$name ($count picture)\n";
} else {
echo "$name ($count pictures)\n";
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With