将.lrc转换为.srt

时间:2016-10-23 16:54:25

标签: php regex preg-match

我想将文件从.lrc转换为.srt(SubRip)

我用:

if (preg_match("|\[(\d\d)\:(\d\d)\.(\d\d)\](.+)|", $lrcl, $m)) {
    $lines[] = array(
        'time' => "00:{$m[1]}:{$m[2]},{$m[3]}0", // convert to SubRip-style time
        'lyrics' => trim($m[4])
    );  
}

$srt .= "$n\n"
    .  "{$lines[$i]['time']} --> {$nexttime}\n"
    .  "{$lines[$i]['lyrics']}\n\n";

我遇到的问题是该行有更多元素:

  

[03:51.91] [02:58.48]

     

[03:13.84] [03:09.29] [03:04.94] [03:00.53]等我回家

     

[03:16.13] [03:11.70] [03:07.19] [03:02.75]

     

[01:32.67]爱可以治愈

您可以看到最后一个元素是歌词,而前一个元素是开始时间。

当我:

echo $lines[$i]['time'];

缺少时间(结果不正确,应按顺序排列):

  

00:03:51910

     

00:03:13840

     

00:03:16130

     

00:01:32670

这是.lrc file

1 个答案:

答案 0 :(得分:0)

一种解决方案是重复用于时间戳的组并使其成为可选:

(\[(\d\d)\:(\d\d)\.(\d\d)\])(?:\[(\d\d)\:(\d\d)\.(\d\d)\])?(?:\[(\d\d)\:(\d\d)\.(\d\d)\])?(?:\[(\d\d)\:(\d\d)\.(\d\d)\])?(.*)

请参阅demo