将一些数组与订单组合

时间:2019-05-15 19:40:07

标签: php arrays foreach

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

$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变量中,我想根据几句话创建一个订单:

Season template is S00
And
Episode template is E00
And
Quality template is 720p and 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结合起来。

1 个答案:

答案 0 :(得分:1)

我添加了名称部分作为第一级,并为每个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字符