public function parse() { $fileContentArray = $this->getFileContentAsArray(); $parsing_errors = array(); $case = 'header'; $i = 1; while (($line = $this->getNextValueFromArray($fileContentArray)) !== false) { // checking header if ('header' === $case) { if (trim($line) == 'WEBVTT') { $case = 'region'; continue; } $parsing_errors[] = 'Missing "WEBVTT" at the beginning of the file'; } if ($case !== 'header') { if ('region' === $case) { // parsing regions if (strpos($line, 'Region:') === 0) { $this->addRegion(WebvttRegion::parseFromString($line)); } else { if (trim($line) === '') { $case = 'body'; } } continue; } else { if ($case === 'body') { // parsing notes if (strpos($line, 'NOTE') === 0) { $note = ''; if (trim($line) !== 'NOTE') { $note = trim(ltrim($line, 'NOTE ')); } $note .= $this->lineEnding; // note continues until there is a blank line while (trim($line = trim($this->getNextValueFromArray($fileContentArray))) !== '') { $note .= $line . $this->lineEnding; $i++; } continue; } // parsing cues $id_match = !strstr($line, '-->') && trim($line) != ''; $matches = array(); $timecode_match = preg_match(self::TIMECODE_PATTERN, $line, $matches); if ($id_match || $timecode_match) { $id = null; $start = null; $stop = null; $settings = null; $text = ''; $note = ''; if ($id_match) { $id = $line; $line = $this->getNextValueFromArray($fileContentArray); $matches = array(); $timecode_match = preg_match(self::TIMECODE_PATTERN, $line, $matches); } if ($timecode_match) { $start = $matches[1]; $stop = $matches[2]; $settings = trim($matches[3]); } else { $parsing_errors[] = 'Malformed cue detected at line ' . $i; } // cue continues until there is a blank line while (trim($line = $this->getNextValueFromArray($fileContentArray)) !== '') { $text .= trim($line) . $this->lineEnding; } // make the cue object and add it to the file $cue = $this->createCue($start, $stop, $text, $settings, $id, $note); $this->addCue($cue); unset($cue); continue; } } } } $i++; } if (count($parsing_errors) > 0) { throw new \Exception('The following errors were found while parsing the file:' . "\n" . print_r($parsing_errors, true)); } return $this; }
public function parse() { $fileContentArray = $this->getFileContentAsArray(); $parsing_errors = array(); $i = 2; // Parse signature. if (rtrim($this->getNextValueFromArray($fileContentArray)) !== 'WEBVTT') { $parsing_errors[] = 'Missing "WEBVTT" at the beginning of the file'; } // Parse regions. while (($line = $this->getNextValueFromArray($fileContentArray)) !== '') { if (strpos($line, 'Region:') === 0) { try { $this->addRegion(WebvttRegion::parseFromString($line)); } catch (\Exception $e) { $parsing_errors[] = $e->getMessage(); } } else { $parsing_errors[] = 'Incorrect Region definition at line ' . $i; } ++$i; } // Skip blank lines after signature. while ($line === '') { $line = $this->getNextValueFromArray($fileContentArray); ++$i; } $note = ''; $id = ''; // Parse cues (comments, ids if they exists). do { // Comment. if (strpos($line, 'NOTE') === 0) { if (trim($line) !== 'NOTE') { $note = trim(ltrim($line, 'NOTE ')); } $note .= $this->lineEnding; // Comment continues until there is a blank line. ++$i; while (trim($line = $this->getNextValueFromArray($fileContentArray)) !== '') { $note .= $line . $this->lineEnding; $i++; } continue; } // Timecode. $matches = array(); $timecode_match = preg_match(self::TIMECODE_PATTERN, $line, $matches); if ($timecode_match) { $start = $matches[1]; $stop = $matches[2]; $settings = isset($matches[3]) ? trim($matches[3]) : ''; // Cue continues until there is a blank line. $text = ''; ++$i; while (trim($line = $this->getNextValueFromArray($fileContentArray)) !== '') { $text .= $line . $this->lineEnding; ++$i; } // Make the cue object and add it to the file. $cue = $this->createCue($start, $stop, $text, $settings, $id, $note); $note = $id = ''; $this->addCue($cue); unset($cue); } elseif ($line !== '') { // Supposse what not empty line before timeline is id. $id = $line; } else { $parsing_errors[] = 'Malformed cue detected at line ' . $i; } ++$i; } while (($line = $this->getNextValueFromArray($fileContentArray)) !== false); if (count($parsing_errors) > 0) { throw new \Exception('The following errors were found while parsing the file:' . "\n" . print_r($parsing_errors, true)); } return $this; }
public function parse() { $handle = fopen($this->filename, "r"); $parsing_errors = array(); if ($handle) { $case = 'header'; $i = 1; while (($line = fgets($handle)) !== false) { // checking header if ($case === 'header' && trim($line) != 'WEBVTT') { $parsing_errors[] = 'Missing "WEBVTT" at the beginning of the file'; } elseif ($case === 'header') { $case = 'region'; continue; } if ($case !== 'header') { // parsing regions if ($case === 'region' && substr($line, 0, 7) == 'Region:') { $this->addRegion(WebvttRegion::parseFromString($line)); continue; } if ($case === 'region' && trim($line) === '') { $case = 'body'; continue; } if ($case === 'body') { // parsing notes if (substr($line, 0, 4) === 'NOTE') { if (trim($line) === 'NOTE') { $note = $this->lineEnding; } else { $note = trim(ltrim($line, 'NOTE ')) . $this->lineEnding; } // note continues until there is a blank line while (trim($line = fgets($handle)) !== '') { $note .= trim($line) . $this->lineEnding; $i++; } continue; } // parsing cues $id_match = !strstr($line, '-->') && trim($line) != ''; $matches = array(); $timecode_match = preg_match(self::TIMECODE_PATTERN, $line, $matches); if ($id_match || $timecode_match) { $id = null; $start = null; $stop = null; $settings = null; $text = ''; if ($id_match) { $id = $line; $line = fgets($handle); $matches = array(); $timecode_match = preg_match(self::TIMECODE_PATTERN, $line, $matches); } if (!$timecode_match) { $parsing_errors[] = 'Malformed cue detected at line ' . $i; } else { $start = $matches[1]; $stop = $matches[2]; $settings = trim($matches[3]); } // cue continues until there is a blank line while (trim($line = fgets($handle)) !== '') { $text .= trim($line) . $this->lineEnding; } // make the cue object and add it to the file $cue = new WebvttCue($start, $stop, $text); $tmp = explode(' ', trim($settings)); foreach ($tmp as $setting) { $tmp2 = explode(':', $setting); if (count($tmp2) !== 2) { continue; } $cue->setSetting($tmp2[0], $tmp2[1]); } if ($id !== null) { $cue->setIdentifier($id); } if (!empty($note)) { $cue->setNote($note); unset($note); } $this->addCue($cue); unset($cue); continue; } } } $i++; } } else { throw new \Exception('Could not read the file "' . $this->filename . '".'); } fclose($handle); if (count($parsing_errors) > 0) { throw new \Exception('The following errors were found while parsing the file:' . "\n" . print_r($parsing_errors, true)); } return $this; }