Hello friends :)
Today i am comming with another solution of my problem that i was facing recently. Actually i was working on an API. Lets clear it more i was working on captionhub API and my requirement was to read the .SRT(Sub Title) file and store all the data of it into an array.
Now some of you want to know that how my subtitle file is looking.
So below is my sample file "GERMAN-TRANSLATION.SRT" data that i put inside a variable $srtFileData.
My Sample Subtitle file data (GERMAN-TRANSLATION.SRT) :
Now i have to read the above file and return all the data into an array. To fullfil this requirement i just create a function by the name of srtToArray.
What you need to do is you just need to pass the above .SRT file data in this function and it will return you an organized array with all the data.
Below is the function-
HOW TO USE:
$srtDataArray = srtToArray($srtFileData);
echo "<pre>";
print_r($srtDataArray);die;
Below is the final output :
Please don't forgot to leave a comment if you like this post. Chears :)
Happy Coding..
Today i am comming with another solution of my problem that i was facing recently. Actually i was working on an API. Lets clear it more i was working on captionhub API and my requirement was to read the .SRT(Sub Title) file and store all the data of it into an array.
Now some of you want to know that how my subtitle file is looking.
So below is my sample file "GERMAN-TRANSLATION.SRT" data that i put inside a variable $srtFileData.
My Sample Subtitle file data (GERMAN-TRANSLATION.SRT) :
$srtFileData = "
1
00:00:01,410 --> 00:00:02,410
Was man schamlos.
2
00:00:07,800 --> 00:00:08,800
Solldas.
3
00:00:08,940 --> 00:00:10,109
Ich wollte gerade Abi im Traum
4
00:00:10,110 --> 00:00:11,629
treffen und du hast mich auf Du
5
00:00:11,940 --> 00:00:12,940
und Dein Olah Abi
6
00:00:14,910 --> 00:00:15,779
du.
7
00:00:15,780 --> 00:00:17,189
Besser mal die Klappe du weißt gar
8
00:00:17,190 --> 00:00:18,190
nicht wie tolle ist.
9
00:00:18,620 --> 00:00:20,069
Er ist ein Rockstar er sieht super
10
00:00:20,070 --> 00:00:21,620
aus. Er singt toll ich meine ist
11
00:00:21,630 --> 00:00:22,630
perfekt weißt du.
12
00:00:23,270 --> 00:00:24,580
Das ist Geschrei und kein Gesang.
13
00:00:25,080 --> 00:00:26,339
Er sieht aus wie ein Affe und hat
14
00:00:26,340 --> 00:00:27,340
Haare wie ein Besen.
15
00:00:27,450 --> 00:00:27,929
Ich möchte ihn.
16
00:00:27,930 --> 00:00:29,189
Am liebsten aus dem Fernseher ziehen
17
00:00:29,190 --> 00:00:29,630
und ihm die.
";
Now i have to read the above file and return all the data into an array. To fullfil this requirement i just create a function by the name of srtToArray.
What you need to do is you just need to pass the above .SRT file data in this function and it will return you an organized array with all the data.
Below is the function-
function srtToArray($srtdata = null)
{
if ($srtdata) {
define('SRT_STATE_SUBNUMBER', 0);
define('SRT_STATE_TIME', 1);
define('SRT_STATE_TEXT', 2);
define('SRT_STATE_BLANK', 3);
define('SPEAKER_SEPRATOR', ':');
$captionHub = array();
$state = SRT_STATE_SUBNUMBER;
$subNum = 0;
$subText = '';
$subTime = '';
$count = 0;
$lines = explode("\n", $srtdata);
foreach($lines as $line) {
switch($state) {
case SRT_STATE_SUBNUMBER:
$subNum = trim($line);
$state = SRT_STATE_TIME;
break;
case SRT_STATE_TIME:
$subTime = trim($line);
$state = SRT_STATE_TEXT;
break;
case SRT_STATE_TEXT:
if (trim($line) == '') {
$tempArray = array();
list($tempArray['timestamp'], $tempArray['end_timestamp']) = explode(' --> ', $subTime);
//Handle if speaker name not added then add n/a.
if (( strpos($subText, SPEAKER_SEPRATOR) == false )) {
$subText = 'N/A'. SPEAKER_SEPRATOR . $subText;
}
list($speaker_name, $speaker_value) = explode(SPEAKER_SEPRATOR, $subText, 2);
$tempArray['value'] = trim($speaker_value);
$subText = '';
$state = SRT_STATE_SUBNUMBER;
$captionHub[trim($speaker_name)]['id'] = $subNum;
$captionHub[trim($speaker_name)]['speaker'] = $subNum;
$captionHub[trim($speaker_name)]['speaker_name'] = trim($speaker_name);
$captionHub[trim($speaker_name)]['elements'][] = $tempArray;
$count++;
} else {
$subText .= $line;
}
break;
}
}
return array_values($captionHub);
}
}
HOW TO USE:
$srtDataArray = srtToArray($srtFileData);
echo "<pre>";
print_r($srtDataArray);die;
Below is the final output :
Array(
[0] => Array
(
[id] => 16
[speaker] => 16
[speaker_name] => N/A
[elements] => Array
(
[0] => Array
(
[timestamp] => 00:00:01,410
[end_timestamp] => 00:00:02,410
[value] => Was man schamlos.
)
[1] => Array
(
[timestamp] => 00:00:07,800
[end_timestamp] => 00:00:08,800
[value] => Solldas.
)
[2] => Array
(
[timestamp] => 00:00:08,940
[end_timestamp] => 00:00:10,109
[value] => Ich wollte gerade Abi im Traum
)
[3] => Array
(
[timestamp] => 00:00:10,110
[end_timestamp] => 00:00:11,629
[value] => treffen und du hast mich auf Du
)
[4] => Array
(
[timestamp] => 00:00:11,940
[end_timestamp] => 00:00:12,940
[value] => und Dein Olah Abi
)
[5] => Array
(
[timestamp] => 00:00:14,910
[end_timestamp] => 00:00:15,779
[value] => du.
)
[6] => Array
(
[timestamp] => 00:00:15,780
[end_timestamp] => 00:00:17,189
[value] => Besser mal die Klappe du wei�t gar
)
[7] => Array
(
[timestamp] => 00:00:17,190
[end_timestamp] => 00:00:18,190
[value] => nicht wie tolle ist.
)
[8] => Array
(
[timestamp] => 00:00:18,620
[end_timestamp] => 00:00:20,069
[value] => Er ist ein Rockstar er sieht super
)
[9] => Array
(
[timestamp] => 00:00:20,070
[end_timestamp] => 00:00:21,620
[value] => aus. Er singt toll ich meine ist
)
[10] => Array
(
[timestamp] => 00:00:21,630
[end_timestamp] => 00:00:22,630
[value] => perfekt wei�t du.
)
[11] => Array
(
[timestamp] => 00:00:23,270
[end_timestamp] => 00:00:24,580
[value] => Das ist Geschrei und kein Gesang.
)
[12] => Array
(
[timestamp] => 00:00:25,080
[end_timestamp] => 00:00:26,339
[value] => Er sieht aus wie ein Affe und hat
)
[13] => Array
(
[timestamp] => 00:00:26,340
[end_timestamp] => 00:00:27,340
[value] => Haare wie ein Besen.
)
[14] => Array
(
[timestamp] => 00:00:27,450
[end_timestamp] => 00:00:27,929
[value] => Ich m�chte ihn.
)
[15] => Array
(
[timestamp] => 00:00:27,930
[end_timestamp] => 00:00:29,189
[value] => Am liebsten aus dem Fernseher ziehen
)
)
)
)
Please don't forgot to leave a comment if you like this post. Chears :)
Happy Coding..