public function render($caption_set, $file = false) { $captions = array(); foreach ($caption_set->captions() as $index => $caption) { $captions[] = sprintf("%d\n%s --> %s\n%s", $index + 1, SrtHelper::time_to_string($caption->start()), SrtHelper::time_to_string($caption->end()), $caption->text()); } $string = implode("\n\n", $captions); if ($file) { return file_put_contents($file, $string); } else { return $string; } }
public function parse() { $split_regex = "/(\\d)+[\r|\n][\r\n]?(\\d{2}:\\d{2}:\\d{2},\\d{3}) --> (\\d{2}:\\d{2}:\\d{2},\\d{3})[\r\n]{1,2}/"; $pieces = preg_split($split_regex, $this->_captions_string, -1, PREG_SPLIT_DELIM_CAPTURE); if (count($pieces) < 4) { throw new Exception("Parse Error: Split created no captions"); } $first = $pieces[0]; if (empty($pieces[0])) { array_shift($pieces); } $captions = new Set(); for ($i = 0, $j = count($pieces); $i < $j; $i += 4) { $caption = new Caption(); $caption->start(SrtHelper::string_to_time($pieces[$i + 1])); $caption->end(SrtHelper::string_to_time($pieces[$i + 2])); $caption->text(trim($pieces[$i + 3])); $captions->add_caption($caption); } $captions->renderer(new SrtRenderer()); return $captions; }
public function test_is_srt() { $this->assertTrue(SrtHelper::is_srt("1\n00:00:00,000 --> 00:01:00,000\nsome test")); $this->assertFalse(SrtHelper::is_srt("00:00:01 --> 00:01:00,000\nsome test")); }