php - 将一些数组与订单组合
问题描述
例如,我在以下变量中有四个链接:
$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 结合起来。
解决方案
我将名称部分添加为第一级和剧集编号,而不是每个 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
字符
推荐阅读
- google-sheets - 将用户名的单单元格列表转换为 Google 表格中的电子邮件单单元格列表
- sql - SQL从只有1个日期的表中选择最大日期
- asp.net - Microsoft SQL Server Native Client 11.0 错误“80040e14”
- laravel - Laravel Eloquent 与数据透视表和条件的关系
- file - 批处理文件 if 和 else 命令保存文件
- c - 里面的“继续”是多余的吗?
- ruby - 为什么 Ruby 会为最后一个未定义的变量在线引发错误?
- amazon-web-services - 俄勒冈州 SNS 主题在 route53 健康 cloudwatch 中不可见
- c - C中的二叉搜索树删除功能
- javascript - 引擎/插件之间共享功能 Ember