php正则表达式读取特定行

php正则表达式读取特定行,php,regex,match,Php,Regex,Match,我有个问题。我试图用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 0

我有个问题。我试图用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);
现在,我尝试将字幕文件的内容放入数组中,如下所示:

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>';
这不好,因为

1
00:00:00,984 --> 00:00:03,503
应该是数组中单个元素中的

我还尝试匹配以下各项(示例):

与:

但它不起作用,我也没有主意了

我想要输出的是:

$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>';
任何帮助都将不胜感激。

这是一个模型:

$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=array(1,'00','1','2,'00','2','abc',3,'00','3','4','00','4','4','5','00','6','00','7');
$string_last=0;//跟踪最后一个元素是字符串的时间
$string_array=array();//添加要保留的元素的新数组
二元=0;
foreach($key=>$value的数组){
if(preg_match('/^\d+/',$value)){//检查行中的第一个字符是否为数字
$string_last=0;//如果是,则最后一个元素不是字符串,请转到下一行
}
//我们有绳子
否则{
如果(!$string_last){$ii++;}//如果最后一个元素不是字符串,则增量索引
else{$string_数组[$ii]。='';}/…否则添加一个空格
$string_数组[$ii]。=$value;
$string_last=1;
}
}
回声';
打印(字符串数组);
回声';

我将把我想要的元素添加到一个新数组中,而不是取消设置我不想要的元素。这样,我可以将连续的字符串元素合并到新数组中的一个元素中。

在读取文件后,可以使用PCRE中的多行修饰符来处理嵌入的换行符;然后匹配不以数字/数字开头的行以获得所需内容:

$subtitles = Subtitles::load('subtitles.srt');
$blocks = $subtitles->getInternalFormat();
$array = [];
foreach ($blocks as $block) {
    $array[] = implode(' ', $block['lines']);
}

print_r($array);

通过使用库,您可以这样做


这是一个干净的方法。
$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>';
$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
)
$subtitles = Subtitles::load('subtitles.srt');
$blocks = $subtitles->getInternalFormat();
$array = [];
foreach ($blocks as $block) {
    $array[] = implode(' ', $block['lines']);
}

print_r($array);