[ create a new paste ] login | about

Link: http://codepad.org/qbprV87P    [ raw code | output | fork ]

PHP, pasted on Dec 23:
<?php

$srt = <<<SRT
1
00:00:12,759 --> 00:00:17,458
"some content here "
that continues here

2
00:00:18,298 --> 00:00:20,926
here we go again...

3
00:00:21,368 --> 00:00:24,565
...and this can go forever...
SRT;


$lines = explode("\r\n", $srt); // simulate file()

$state = 0;
$blocks = array();
foreach($lines as $line)
{
	switch($state)
	{
		case 0:
			unset($block);
			$block = array();
			$blocks[] = &$block;
			$block['number'] = $line;
			$state = 1;
			break;
		case 1:
			$block['range'] = $line;
			$state = 2;
			break;
		case 2:
			$block['text'] = '';
			$state = 3;
			# fall-through intended
		case 3:
			if ($line === '') {
				$state = 0;
				break;
			}
			$block['text'] .= ($block['text'] ? "\n" : '') . $line;
			break;
		default:
			throw new Exception(sprintf('Unhandled %d.', $state));
	}
}
unset($block);
print_r($blocks);


Output:
Array
(
    [0] => Array
        (
            [number] => 1
            [range] => 00:00:12,759 --> 00:00:17,458
            [text] => "some content here "
that continues here
        )

    [1] => Array
        (
            [number] => 2
            [range] => 00:00:18,298 --> 00:00:20,926
            [text] => here we go again...
        )

    [2] => Array
        (
            [number] => 3
            [range] => 00:00:21,368 --> 00:00:24,565
            [text] => ...and this can go forever...
        )

)


Create a new paste based on this one


Comments: