I have something like this in my directory:
slider-1.jpg
slider-2.png
slider-4.gif
slider-8.png
slider-11.gif
Now is there a way to get the last trailing number of "slider" images ? I need to get this so the next uploaded image should be named slider-12.xxx
I tried for each loop, but it obviously gets me slider-3 as a next name which is false in my case :)
ok, here is how I did it
foreach ($files as $filename) {
$filesarr[]= $filename;
$namearr = explode('.', $filename); //I had to grab this piece as well during the project, so this is main reason not usin Louis proposal.... i needed that array for something else...
$numbers = explode('-', $namearr[1]);
$compare[]=$numbers[2];
}
sort($compare, SORT_NUMERIC);
$assigned_number = end($compare) + 1;
You could try something like this:
$max = 0;
foreach($files AS $file) {
$max = max($max, filter_var($file, FILTER_SANITIZE_NUMBER_INT));
}
$max++;
or this:
natcasesort($files);
$max = filter_var(array_slice($files, -1), FILTER_SANITIZE_NUMBER_INT) + 1;