我有问题。我试图用php计算字幕行数。 您可能知道,副标题如下所示:
1
00:00:00,984 --> 00:00:03,503
All right, guys, let's get to it.
2
00:00:03,587 --> 00:00:04,821
What's that button?
3
00:00:04,872 --> 00:00:07,590
It's something designed
to help you get healthy.
4
00:00:07,658 --> 00:00:09,676
Just ignore it.
5
00:00:09,760 --> 00:00:12,962
So, Patrick, did you take the high road
现在,我试图将字幕文件的内容放在一个数组中,如下所示:
$f = fopen($file, 'rb');
$read = fread($f, filesize($file));
fclose($f);
$array = explode("\n",$read);
使用此代码:
$array = array_filter($array,'trim');
foreach($array as $key => $value) {
if(preg_match('/\d+/',$value)) {
unset($array[$key]);
}
}
$array = array_values($array);
echo '<pre>';
print_r($array);
echo '</pre>';
我得到:
Array
(
[0] => All right, guys, let's get to it.
[1] => What's that button?
[2] => It's something designed
[3] => to help you get healthy.
[4] => Just ignore it.
[5] => So, Patrick, did you take the high road
[6] => and congratulate Wendy on that promotion
[7] => that you were supposed to get?
[8] => Yes, I did. I even bought her flowers.
[9] => Liar!
)
这不好,因为
It's something designed
to help you get healthy.
应该是数组中的单个元素。
我还尝试匹配(示例)之间的所有内容:
1
00:00:00,984 --> 00:00:03,503
和
2
00:00:03,587 --> 00:00:04,821
使用:
(\d+\n)([0-9][0-9]:[0-9][0-9]:[0-9][0-9],\d+ --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],\d+\n).*\n
但它不起作用,我没有想法。
我要输出的内容:
Array
(
[0] => All right, guys, let's get to it.
[1] => What's that button?
[2] => It's something designed to help you get healthy.
[3] => Just ignore it.
[4] => So, Patrick, did you take the high road
[5] => and congratulate Wendy on that promotion that you were supposed to get?
[6] => Yes, I did. I even bought her flowers.
[7] => Liar!
)
echo count($array); //for the previous array , should echo 8
任何帮助将不胜感激。
答案 0 :(得分:2)
您可以使用PCRE中的多线修改器在读取文件后处理嵌入的换行符;然后匹配不以数字/数字开头的行来获得你想要的东西:
$file = "./subtitles.txt";
$content = file_get_contents($file);
$blocks = preg_split('/^\s*$/m', $content);
// var_export($blocks);
$subtitles = array();
for ($i=0; $i < count($blocks); $i++) {
$lines = explode("\n", $blocks[$i]);
$matches = preg_grep("/^[^\d]/", $lines);
array_push($subtitles, implode(' ', $matches));
}
print_r($subtitles);
它为您提供以下输出:
Array
(
[0] => All right, guys, let's get to it.
[1] => What's that button?
[2] => It's something designed to help you get healthy.
[3] => Just ignore it.
[4] => So, Patrick, did you take the high road
)
答案 1 :(得分:0)
这是一个模拟:
$array = array(1, '00', 'one', 2, '00', 'two', 'abc', 3, '00', 'three', 4, '00', 'four', 'five', 5, '00', 'six', 6, '00', 'seven');
$string_last = 0; // keep track when last element was string
$string_array = array(); // new array to add elements I want to keep
$ii = 0;
foreach($array as $key => $value) {
if(preg_match('/^\d+/',$value)) { // check if first character in line is a digit
$string_last = 0; // if so, then last element is not string, go to next line
}
// we have string line
else {
if ( !$string_last ) { $ii++; } // if last element was not a string, increment index
else { $string_array[$ii] .= ' '; } // ...otherwise add a space
$string_array[$ii] .= $value;
$string_last = 1;
}
}
echo '<pre>';
print_r($string_array);
echo '</pre>';
而不是取消设置元素,我不希望我添加我想要的新元素的元素。这样我就可以将连续的字符串元素合并到我的新数组中的一个元素中。
答案 2 :(得分:0)
您可以使用库https://github.com/mantas-done/subtitles
这样做$subtitles = Subtitles::load('subtitles.srt');
$blocks = $subtitles->getInternalFormat();
$array = [];
foreach ($blocks as $block) {
$array[] = implode(' ', $block['lines']);
}
print_r($array);