Skip to content

Commit

Permalink
Merge pull request #14 from mtbksouth/patch-1
Browse files Browse the repository at this point in the history
fixes issue with multi-line captions
  • Loading branch information
benlipp authored Jun 23, 2017
2 parents 86dd41e + b681d7c commit e87bb5f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/SrtParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function loadFile($file)

return $this;
}

public function loadString($string)
{
$this->data = $string;
Expand All @@ -43,21 +43,24 @@ public function parse()
*/
private static function splitData($data)
{
$sections = explode("\r\n\r\n", $data);
//find digits followed by a single line break and timestamps
$sections = preg_split('/\d+(?:\r\n|\r|\n)(?=(?:\d\d:\d\d:\d\d,\d\d\d)\s-->\s(?:\d\d:\d\d:\d\d,\d\d\d))/m', $data,-1,PREG_SPLIT_NO_EMPTY);
$matches = [];
foreach ($sections as $section) {
$matches[] = explode("\r\n", $section, 3);
//cleans out control characters, borrowed from https://stackoverflow.com/a/23066553
$section = preg_replace('/[^\PC\s]/u', '', $section);
if(trim($section) == '') continue;
$matches[] = preg_split('/(\r\n|\r|\n)/', $section, 2,PREG_SPLIT_NO_EMPTY);
}

return $matches;
}

private static function buildCaptions($matches)
{
$captions = [];
foreach ($matches as $match) {
$times = self::timeMatch($match[1]);
$text = self::textMatch($match[2]);
$times = self::timeMatch($match[0]);
$text = self::textMatch($match[1]);

$captions[] = new Caption($times['start_time'], $times['end_time'], $text);
}
Expand Down

0 comments on commit e87bb5f

Please sign in to comment.