dpbvpgvrhwxen3222 2010-07-27 05:29
浏览 60
已采纳

如何优化从php中的输入匹配多部分rar文件的算法

I'm looking for a better optimized way to find and group multipart archives from an array of filenames

I have as an input for example:

array(
books.part1.rar,
books.part3.rar,
00000114.rar,
svoy_20ostrov.rar,
svoy_20ostrov.rar,
koncert_20v_20dk_20mir.rar,
koncert_20v_20centralnom_20teatre_20kukol.rar,
LP_LIVE_PR_Tampa.part2.rar,
koncert_20v_20dk_20vami.rar,
koncert_20v_20dk_20kommuna_20chast1.rar,
books.part2.rar,
koncert_20v_20dk_20kommuna_20chast2.rar,
books.part4.rar,
recedivist.rar,
LP_LIVE_PR_Tampa.part1.rar
)

And I'm looking for the output

array(  

array(

books.part1.rar
books.part2.rar
books.part3.rar
books.part4.rar ) ,

00000114.rar
svoy_20ostrov.rar
koncert_20v_20dk_20mir.rar
koncert_20v_20centralnom_20teatre_20kukol.rar
koncert_20v_20dk_20vami.rar

array(
koncert_20v_20dk_20kommuna_20chast1.rar
koncert_20v_20dk_20kommuna_20chast2.rar
)

recedivist.rar
array (
LP_LIVE_PR_Tampa.part1.rar
LP_LIVE_PR_Tampa.part2.rar
)
)

I'm using php as a programming language, by the way,

An idea was to match with a regular expression files like (.+).part1.rar then when found , match all the other part([0-9]+).rar (other foreach required that loops through all array) and when found unset() those entries and add them to the new constructed array

  • 写回答

2条回答 默认 最新

  • doucaishou0074 2010-07-27 06:18
    关注

    I would sort the array first and then loop through it, performing the Levenshtein() function on the next entry.

    $rars = array(
        books.part1.rar,
        books.part3.rar,
        00000114.rar,
        svoy_20ostrov.rar,
        svoy_20ostrov.rar,
        koncert_20v_20dk_20mir.rar,
        koncert_20v_20centralnom_20teatre_20kukol.rar,
        LP_LIVE_PR_Tampa.part2.rar,
        koncert_20v_20dk_20vami.rar,
        koncert_20v_20dk_20kommuna_20chast1.rar,
        books.part2.rar,
        koncert_20v_20dk_20kommuna_20chast2.rar,
        books.part4.rar,
        recedivist.rar,
        LP_LIVE_PR_Tampa.part1.rar
    )
    
    sort($rars);
    $current = 0;
    $rars_complete = array();
    foreach($rars as $i=>$rar) {
        $next = ($i + 1) < count($rars)) ? $i + 1 : false;
        $rars_complete[$current][] = $rar;
        if($next != false && levenshtein($rar, $rars[$next]) == 1)
            continue;
        else
            $current++;
    }
    

    Note, this is not tested.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理