如何用不同的命名查找同一图像的最短名称(字符串) [英] How to find a shortest name (string) of the same image with different naming

查看:71
本文介绍了如何用不同的命名查找同一图像的最短名称(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我不知道该如何处理的问题. 我有一个包含不同图像的文件夹.相同的图像有不同的名称.我需要找到最短的一个.

I have a problem that I don´t know how to approach. I have a folder of different images. There are different names for the same images. I need to find the shortest one.

一个图像可以具有不同的名称.例如这样的

One image can have different names. For example like this:

  • clownfish.jpg,clownfish-1024x658.jpg,clownfish-150x150.jpg =>需要找到 clownfish.jpg
  • clownfish.jpg, clownfish-1024x658.jpg, clownfish-150x150.jpg => need to find clownfish.jpg

另一张图片:

  • b800-768x575.jpg,b800-4.jpg,b800.jpg =>我需要找到 b800.jpg

还有另一张图片:

  • agility_3.jpg,agility_3-45x45.jpg =>我需要找到 agility_3.jpg

所有图像都在同一文件夹中.如何区分这些图像并选择同一图像组并找到最短的图像? 对不起,但我不知道如何更好地解释它. 有什么想法吗?

All images are in the same folder. How can I differentiate between those images and select the group of same images and find the shortest one? I´m sorry but I don´t know how to explain it better. Any ideas?

为此,我将使用php.

For doing this I will use php.

推荐答案

您可以使用 levenshtein 函数比较字符串并使用它来查找最小的相似字符串.
以下代码中的注释.

You can use levenshtein function to compare the strings and use that to find the smallest similar string.
Comments in code below.

您可以使用 scandir 来获取文件数组.

You can use scandir to get the array of files.

$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似乎想搜索文件,如果找不到,则从文件夹中查找原始名称.

Below in comments it seems OP wants to search for a file, if not found find original name from folder.

$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

这篇关于如何用不同的命名查找同一图像的最短名称(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆