0

PHP を使用してテキスト ファイルをクリーンアップするためのサポートが必要です。ファイルは、特定の方法でテキストをフォーマットする必要がある別の関数によって後で処理されています。

元のクローズド キャプション テキスト:

1
00:00:22,767 --> 00:00:24,634
line text 1
line text 2
line text 3

2
00:00:26,767 --> 00:00:28,634
line text 1
line text 2
line text 3

行テキストをすべて 1 行で入力する必要があります。例えば。

1
00:00:22,767 --> 00:00:24,634
line text 1 line text 2 line text 3

2
00:00:26,767 --> 00:00:28,634
line text 1 line text 2 line text 3

ヘルプ/入力が欲しいです。正しいヘッドスペースに入るのに苦労しています。ありがとう。

4

1 に答える 1

0

次のようにファイルを読み取ることができます: Read a plain text file with php そして、各行を処理し、別のファイルに書き込みます。変更によって元のファイルを上書きしたい場合は、そのコピーを作成し、コピーから読み取り、元のファイルに変更を書き込むことができます。このようなものが動作するはずです:

<?php

$oldFile = fopen('oldFile.txt','r');
$newFile = fopen('newFile.txt', 'w');
$newLine = false;
while ($line = fgets($oldFile)) {
    //If is the number of the caption
    if(preg_match('/^\d+$/',$line)) {
        if(!newLine) {
            fwrite($newFile,'\n');  
        }
        fwrite($newFile, $line.'\n');
        $newLine = true;
    }
    //if it is the minutes label
    //00:00:22,767 --> 00:00:24,634
    else if(preg_match('/^\d{2}:\d{2}:\d{2}.\d{3} --> \d{2}:\d{2}:\d{2}.\d{3}$/',$line)) {
        if(!newLine) {
            fwrite($newFile,'\n');  
        }
        fwrite($newFile, $line.'\n');
        $newLine = true;
    }
    else {
        fwrite($newFile,$line.' ');
        $newLine = false;
    }
}
fclose($newFile)
fclose($oldFile);
?>
于 2014-02-12T20:06:40.853 に答える