0

我的目录中有这样的东西:

slider-1.jpg
slider-2.png
slider-4.gif
slider-8.png
slider-11.gif

现在有没有办法获得最后一个尾随数量的“滑块”图像?我需要得到这个,所以下一个上传的图像应该命名为slider-12.xxx

我尝试了每个循环,但它显然让我将滑块 3 作为下一个名称,在我的情况下这是错误的 :)

好的,这就是我的做法

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;
4

1 回答 1

4

你可以尝试这样的事情:

$max = 0;
foreach($files AS $file) {
    $max = max($max, filter_var($file, FILTER_SANITIZE_NUMBER_INT));
}
$max++;

或这个:

natcasesort($files);
$max = filter_var(array_slice($files, -1), FILTER_SANITIZE_NUMBER_INT) + 1;
于 2012-12-20T09:04:13.120 回答