Вы можете использовать функцию levenshtein для сравнения строк и использовать эту функцию, чтобы найти наименьшую похожую строку.
Комментарии в коде ниже.
Вы можете использовать scandir , чтобы получить массив файлов.
$arr= ["clownfish.jpg", "clownfish-1024x658.jpg", "clownfish-150x150.jpg",
"b800-768x575.jpg", "b800-4.jpg", "b800.jpg",
"agility_3.jpg", "agility_3-45x45.jpg"];
foreach($arr as $key => $item){
foreach($arr as $key2 => $item2){
if($key != $key2){// make sure we don't compare the same items
$length = min(strlen($item), strlen($item2))-4; // lenght of the smallest string - ".jpg"
if(levenshtein(substr($item,0,$length), substr($item2,0,$length), 1,1,0) == 0){ //is it the same file
$len = [strlen($item) => $item, strlen($item2) => $item2]; // make a associative array with lenght as key
$minkey = min(array_keys($len)); // find the smallest of them
$new[] = $len[$minkey]; // add the smallest to the new array
}
}
}
}
var_dump(array_unique($new)); // remove duplicates
Вывод вышеуказанного
array(3) {
[0]=>
string(13) "clownfish.jpg"
[4]=>
string(8) "b800.jpg"
[8]=>
string(13) "agility_3.jpg"
}
https://3v4l.org/q52vs
Ниже в комментариях кажется, что OP хочет искать файл, если не найден, найти оригинальное имя из папки.
$find = "clownfish-1024x658.jpg";
var_dump(in_array($find, $arr)); // does file exist in array
foreach($arr as $key => $item){
$length = min(strlen($item), strlen($find))-4; // lenght of the smallest string - ".jpg"
if(levenshtein(substr($item,0,$length), substr($find,0,$length), 1,1,0) == 0){ //is it the same file
$len = [strlen($item) => $item, strlen($find) => $find]; // make a associative array with lenght as key
$minkey = min(array_keys($len)); // find the smallest of them
$new[] = $len[$minkey]; // add the smallest to the new array
}
}
var_dump(array_unique($new));
https://3v4l.org/HRKMa