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; }