Skip to content

Commit

Permalink
Make srt more flexible with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Jun 27, 2023
1 parent 90dda54 commit 40890d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Code/Converters/SrtConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SrtConverter implements ConverterContract
{
public function canParseFileContent($file_content)
{
return preg_match('/^\d?\R(\d{2}:\d{2}:\d{2},\d{3}\s-->\s\d{2}:\d{2}:\d{2},\d{3})\R(.+)$/m', $file_content) === 1;
return preg_match('/^\d?\R(\d{2}:\d{2}:\d{2},\d{3}\s*-->\s*\d{2}:\d{2}:\d{2},\d{3})\R(.+)$/m', $file_content) === 1;
}

/**
Expand All @@ -21,7 +21,7 @@ public function fileContentToInternalFormat($file_content)

$blocks = explode("\n\n", trim($file_content)); // each block contains: start and end times + text
foreach ($blocks as $block) {
preg_match('/(?<start>.*) --> (?<end>.*)\n(?<text>(\n*.*)*)/m', $block, $matches);
preg_match('/(?<start>.*) *--> *(?<end>.*)\n(?<text>(\n*.*)*)/m', $block, $matches);

// if block doesn't contain text (invalid srt file given)
if (empty($matches)) {
Expand Down
19 changes: 18 additions & 1 deletion tests/formats/SrtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,31 @@ public function testRemovesEmptyLines()
Very good, Lieutenant.
TEXT;

$actual_format = Subtitles::loadFromString($content, 'srt')->getInternalFormat();
$actual_format = Subtitles::loadFromString($content)->getInternalFormat();
$expected_format = (new Subtitles())
->add(3, 4, ['Very good, Lieutenant.'])
->getInternalFormat();
$this->assertEquals($expected_format, $actual_format);

}

public function testParsesClientFile()
{
$content = <<< TEXT
1
00:00:01,000-->00:00:02,000
1
TEXT;

$actual_format = Subtitles::loadFromString($content)->getInternalFormat();
$expected_format = (new Subtitles())
->add(1, 2, ['1'])
->getInternalFormat();
$this->assertEquals($expected_format, $actual_format);

}

// ---------------------------------- private ----------------------------------------------------------------------

private static function fileContent()
Expand Down

0 comments on commit 40890d2

Please sign in to comment.