public function parse() { $matches = array(); $res = preg_match($this->options['_requireStrictFileFormat'] ? self::PATTERN_STRICT : self::PATTERN_LOOSE, $this->fileContent, $matches); if ($res === false || $res === 0) { throw new \Exception($this->filename . ' is not a proper .srt file.'); } $this->setLineEnding($matches[1]); $bom = pack('CCC', 0xef, 0xbb, 0xbf); $matches = explode($this->lineEnding . $this->lineEnding, trim($matches[0], $bom . $this->lineEnding)); $subtitleOrder = 1; $subtitleTime = ''; foreach ($matches as $match) { $subtitle = explode($this->lineEnding, $match, 3); $timeline = explode(' --> ', $subtitle[1]); $subtitleTimeStart = $timeline[0]; $subtitleTimeEnd = $timeline[1]; if (!$this->options['_requireStrictFileFormat']) { $subtitleTimeStart = $this->cleanUpTimecode($subtitleTimeStart); $subtitleTimeEnd = $this->cleanUpTimecode($subtitleTimeEnd); } if ($subtitle[0] != $subtitleOrder++ || !$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd, !$this->options['_requireStrictFileFormat']) || $this->options['_requireStrictFileFormat'] && !$this->validateTimelines($subtitleTime, $subtitleTimeStart, true)) { throw new \Exception($this->filename . " failed timeline validation. ({$subtitleTimeStart} --> {$subtitleTimeEnd})"); } $subtitleTime = $subtitleTimeEnd; $cue = new SubripCue($timeline[0], $timeline[1], $subtitle[2]); $cue->setLineEnding($this->lineEnding); $this->addCue($cue); } return $this; }
public function parse() { $matches = array(); if ($this->softmode) { $res = preg_match(self::PATTERN_SOFTMODE, trim($this->fileContent), $matches); } else { $res = preg_match(self::PATTERN, $this->fileContent, $matches); } if ($res === false || $res === 0) { throw new \Exception($this->filename . ' is not a proper .srt file.'); } $this->setLineEnding($matches[1]); $bom = pack('CCC', 0xef, 0xbb, 0xbf); $matches = explode($this->lineEnding . $this->lineEnding, trim($matches[0], $bom . $this->lineEnding)); $subtitleOrder = 1; $subtitleTime = ''; foreach ($matches as $match) { $subtitle = explode($this->lineEnding, $match, 3); $timeline = explode(' --> ', $subtitle[1]); $subtitleTimeStart = $timeline[0]; $subtitleTimeEnd = $timeline[1]; if (!$this->softmode && ($subtitle[0] != $subtitleOrder++ || !$this->validateTimelines($subtitleTime, $subtitleTimeStart, true) || !$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd))) { throw new \Exception($this->filename . ' is not a proper .srt file.'); } $subtitleTime = $subtitleTimeEnd; $cue = new SubripCue($timeline[0], $timeline[1], isset($subtitle[2]) ? $subtitle[2] : ''); $cue->setLineEnding($this->lineEnding); $this->addCue($cue); } return $this; }
public function parse() { $matches = array(); $res = preg_match_all(self::PATTERN, $this->fileContent, $matches); if (!$res || $res == 0) { throw new \Exception($this->filename . ' is not a proper .srt file.'); } $entries_count = count($matches[1]); for ($i = 0; $i < $entries_count; $i++) { $cue = new SubripCue($matches[1][$i], $matches[2][$i], $matches[3][$i]); $cue->setLineEnding($this->lineEnding); $this->addCue($cue); } return $this; }
public function parse() { $matches = array(); $res = preg_match(self::PATTERN, $this->fileContent, $matches); if ($res === false || $res === 0) { throw new \Exception($this->filename . ' is not a proper .srt file.'); } $this->setLineEnding($matches[1]); $bom = pack('CCC', 0xef, 0xbb, 0xbf); $matches = explode($this->lineEnding . $this->lineEnding, trim($matches[0], $bom . $this->lineEnding)); $subtitleOrder = 1; $subtitleTime = ''; foreach ($matches as $match) { $subtitle = explode($this->lineEnding, $match, 3); $timeline = explode(' --> ', $subtitle[1]); $subtitleTimeStart = $timeline[0]; $subtitleTimeEnd = $timeline[1]; if ($subtitle[0] != $subtitleOrder++ || !$this->validateTimelines($subtitleTime, $subtitleTimeStart, true) || !$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd)) { switch (true) { case $subtitle[0] != $subtitleOrder - 1: $errorMsg = 'Invalid subtitle order index: ' . $subtitle[0]; break; case !$this->validateTimelines($subtitleTime, $subtitleTimeStart, true): $errorMsg = 'Staring time invalid: ' . $subtitleTimeStart; break; case !$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd): $errorMsg = 'Ending time invalid: ' . $subtitleTimeEnd; break; } throw new \Exception($this->filename . ' is not a proper .srt file. (' . $errorMsg . ')'); } $subtitleTime = $subtitleTimeEnd; $cue = new SubripCue($timeline[0], $timeline[1], $subtitle[2]); $cue->setLineEnding($this->lineEnding); $this->addCue($cue); } return $this; }
public static function ms2tc($ms) { return SubripCue::ms2tc($ms, '.'); }
public static function tc2ms($tc) { return SubripCue::tc2ms($tc . '0'); }
public static function ttml2subrip(TtmlFile $_ttml) { $srt = new SubripFile(); foreach ($_ttml->getCues() as $cue) { $text = $cue->getText(); if (null !== $cue->getStyle()) { $cueStyle = $_ttml->getStyle($cue->getStyle()); // global cue style if (isset($cueStyle['fontStyle']) && 'italic' === $cueStyle['fontStyle']) { $text = '<i>' . $text . '</i>'; } if (isset($cueStyle['fontWeight']) && 'bold' === $cueStyle['fontWeight']) { $text = '<b>' . $text . '</b>'; } if (isset($cueStyle['textDecoration']) && 'underline' === $cueStyle['textDecoration']) { $text = '<u>' . $text . '</u>'; } // span styles $matches = array(); preg_match_all('#<span[^>]*style="([^>"]+)"[^>]*>(.+)</span>#isU', $text, $matches); $spanCount = count($matches[0]); if ($spanCount > 0) { for ($i = 0; $i < $spanCount; $i++) { $spanStr = $matches[0][$i]; $spanStyleId = $matches[1][$i]; $spanText = $matches[2][$i]; $spanStyle = $_ttml->getStyle($spanStyleId); if (isset($spanStyle['fontStyle']) && 'italic' === $spanStyle['fontStyle']) { $text = str_replace($spanStr, '<i>' . $spanText . '</i>', $text); } if (isset($spanStyle['fontWeight']) && 'bold' === $spanStyle['fontWeight']) { $text = str_replace($spanStr, '<b>' . $spanText . '</b>', $text); } if (isset($spanStyle['textDecoration']) && 'underline' === $spanStyle['textDecoration']) { $text = str_replace($spanStr, '<u>' . $spanText . '</u>', $text); } } } } if (null !== $cue->getRegion()) { $cueRegion = $_ttml->getRegion($cue->getRegion()); // global cue style if (isset($cueRegion['fontStyle']) && 'italic' === $cueRegion['fontStyle']) { $text = '<i>' . $text . '</i>'; } if (isset($cueRegion['fontWeight']) && 'bold' === $cueRegion['fontWeight']) { $text = '<b>' . $text . '</b>'; } if (isset($cueRegion['textDecoration']) && 'underline' === $cueRegion['textDecoration']) { $text = '<u>' . $text . '</u>'; } } $text = str_ireplace(array('<br>', '<br/>', '<br />'), SubripFile::UNIX_LINE_ENDING, $text); $cleaningPatterns = array('</i>' . SubripFile::UNIX_LINE_ENDING . '<i>', '</b>' . SubripFile::UNIX_LINE_ENDING . '<b>', '</u>' . SubripFile::UNIX_LINE_ENDING . '<u>'); $text = str_ireplace($cleaningPatterns, SubripFile::UNIX_LINE_ENDING, $text); $srt->addCue($text, SubripCue::ms2tc($cue->getStartMS()), SubripCue::ms2tc($cue->getStopMS())); } return $srt; }
public static function ttml2subrip(TtmlFile $_ttml) { $srt = new SubripFile(); foreach ($_ttml->getCues() as $cue) { $text = $cue->getText(); if (null !== $cue->getStyle()) { // global cue style $text = self::applyTtmlStyles($text, $_ttml->getStyle($cue->getStyle())); // span styles $matches = array(); preg_match_all('#<span[^>]*style="([^>"]+)"[^>]*>(.+)</span>#isU', $text, $matches); $spanCount = count($matches[0]); if ($spanCount > 0) { for ($i = 0; $i < $spanCount; $i++) { $spanStr = $matches[0][$i]; $spanStyleId = $matches[1][$i]; $spanText = $matches[2][$i]; $spanStyle = $_ttml->getStyle($spanStyleId); $textForReplace = self::applyTtmlStyles($spanText, $spanStyle); if ($textForReplace != $spanText) { $text = str_replace($spanStr, $textForReplace, $text); } } } } if (null !== $cue->getRegion()) { // cue region style $text = self::applyTtmlStyles($text, $_ttml->getRegion($cue->getRegion())); } $text = str_ireplace(array('<br>', '<br/>', '<br />'), SubripFile::UNIX_LINE_ENDING, $text); $text = preg_replace('#<\\/?span[^>]*>#i', '', $text); $cleaningPatterns = array('</i>' . SubripFile::UNIX_LINE_ENDING . '<i>', '</b>' . SubripFile::UNIX_LINE_ENDING . '<b>', '</u>' . SubripFile::UNIX_LINE_ENDING . '<u>'); $text = html_entity_decode(str_ireplace($cleaningPatterns, SubripFile::UNIX_LINE_ENDING, $text)); $srt->addCue($text, SubripCue::ms2tc($cue->getStartMS()), SubripCue::ms2tc($cue->getStopMS())); } return $srt; }