首页 > 解决方案 > 将一些数组与订单组合

问题描述

例如,我在以下变量中有四个链接:

$content = "

http://example.com/folder/Name.S01E01.720p.mp4
http://example.com/folder/Name.S01E02.720p.mp4

http://example.com/folder/Name.S02E01.480p.mp4
http://example.com/folder/Name.S02E02.480p.mp4

";

在 up 变量中,我想根据几句话创建一个订单:

季节模板为S00
和
剧集模板为E00
和
质量模板为720p 和 480p

所以,我需要结合这些值来得到一个结果,

我需要一些函数来使 $content 变成这样:

Array
(
    [01] => //Season Number
    (
        [720p] =>//Quality Name
            (
                [URLs] => //Full Url's
                    (
                        [0] => http://example.com/folder/Name.S01E01.720p.mp4
                        [1] => http://example.com/folder/Name.S01E02.720p.mp4
                    )

            )

    )

[02] => //Season Number
    (
        [480p] => //Quality Name
            (
                [URLs] => //Full Url's
                    (
                        [0] => http://example.com/folder/Name.S02E01.480p.mp4
                        [1] => http://example.com/folder/Name.S02E02.480p.mp4
                    )

            )

    )

)

我希望你明白我的意思。

请看这个:

$content = "

http://example.com/folder/Name.S01E01.720p.mp4
http://example.com/folder/Name.S01E02.720p.mp4

http://example.com/folder/Name.S02E01.480p.mp4
http://example.com/folder/Name.S02E02.480p.mp4

";


if(preg_match_all('/(https?:\/\/[^ ]+?(?:\.mkv|\.mp4))/ms', $content, $matches)){
    foreach($matches[0] as $link){
        if(preg_match('/(\d++(p))/i',$link,$q)){
            $quality[] = $q[0];
        }
        $full_url[] = trim(urldecode($link));
    }
}

if(preg_match_all('/(S(\d++)E(\d++))/i', $content, $parts)){
    foreach($parts[0] as $part){
         $season[]  = $part[2];
    }
}

//var_dump($quality);
//var_dump($full_url);
//var_dump($season);

我想将 $season 与 $full_url 和 $quality 结合起来。

标签: phparraysforeach

解决方案


我将名称部分添加为第一级和剧集编号,而不是每个 URL 的动态索引,如果您不想要它们,则只需使用$result[$m[2]][$m[4]]['URLs'][] = $url;. .mp4如果您有其他扩展,我也省略了该部分:

$lines = array_filter(explode("\n", $content));
foreach($lines as $url) {
    preg_match('/^([^.]+)\.S(\d\d)E(\d\d)\.(\d+p)/', pathinfo($url, PATHINFO_BASENAME), $m);
    $result[$m[1]][$m[2]][$m[4]]['URLs'][$m[3]] = $url;
}
  • 拆分行尾的内容并过滤掉空行
  • 循环这些行并从基本文件名中匹配您想要的部分
  • 使用这些匹配构建多维数组

鉴于您的$content这将产生以下结果:

Array
(
    [Name] => Array
        (
            [01] => Array
                (
                    [720p] => Array
                        (
                            [URLs] => Array
                                (
                                    [01] => http://example.com/folder/Name.S01E01.720p.mp4
                                    [02] => http://example.com/folder/Name.S01E02.720p.mp4
                                )
                        )
                )
            [02] => Array
                (
                    [480p] => Array
                        (
                            [URLs] => Array
                                (
                                    [01] => http://example.com/folder/Name.S02E01.480p.mp4
                                    [02] => http://example.com/folder/Name.S02E02.480p.mp4
                                )
                        )
                )
        )
)
  • 从头开始匹配^并捕获一个或多个+非点[^.]字符
  • 然后匹配一个点\.字母S并捕获两个数字\d\d
  • 然后匹配字母E并捕获两个数字\d\d
  • 然后匹配一个点\.并捕获一个或多个数字\d+p字符

推荐阅读