1

次の形式のSRTファイルのディレクトリがあります。

8
00:00:45,879 --> 00:00:50,680
- Oh! Just leave me in the car with the window open a crack.
- That's the plan.

9
00:00:50,784 --> 00:00:54,117
I think it's nice we're doing something
Maggie will enjoy for once.

10
00:00:54,220 --> 00:00:58,350
Besides, I'm sure Storytown Village
is also fun for everyone...

特定の値をMySQLデータベースに取り込もうとしていますが、正規表現とphpを使用してそれを行う方法に完全に困惑しています。

初めて(つまり00:00:50)を「時間」に入れ、その時間に関連するテキスト行を「テキスト」に入れたいと思います。

これを行うためのより簡単な方法がある場合、正規表現が進むべき道であるかどうかさえ100%確信していませんか?

4

2 に答える 2

1

テキスト内に多くの区切り文字があるので、正規表現は使用しません。文字列操作を使用した解決策は次のとおりです。

$lines = explode( "\n", $str);

for( $i = 0, $ii = count( $lines); $i < $ii; $i += 5) {
    $num = trim( $lines[ $i ]);

    list( $line2a, $line2b) = explode( ' --> ', $lines[ $i + 1]);
    list( $time1, $val1) = explode( ',', $line2a);
    list( $time2, $val2) = explode( ',', $line2b);

    $text1 = $lines[ $i + 2];
    $text2 = $lines[ $i + 3];

    echo "$num $time1 $val1 $time2 $val2\n$text1\n$text2\n\n";
}

どの変数がファイルのどの値に割り当てられているかを確認するには、デモを参照してください。

于 2012-06-23T01:43:24.300 に答える
0

このパターンは機能します:

$pattern = '/([\d,:]+) --> [\d,:]+\n(.*\n.*)[^\n\n]/m';
$string = "
8
00:00:45,879 --> 00:00:50,680
- Oh! Just leave me in the car with the window open a crack.
- That's the plan.

9
00:00:50,784 --> 00:00:54,117
I think it's nice we're doing something
Maggie will enjoy for once.

10
00:00:54,220 --> 00:00:58,350
Besides, I'm sure Storytown Village
is also fun for everyone..."; //Your File Contents Here

preg_match_all($pattern, $string, $matches);
print_r($matches);

これにより、次のようになります。

Array
(
[0] => Array
    (
        [0] => 00:00:45,879 --> 00:00:50,680

- おー!窓を開けたまま車の中に置いておいてください。-それが計画です。[1] => 00:00:50,784-> 00:00:54,117マギーが一度楽しんでくれることをやっているのはいいことだと思います。[2] => 00:00:54,220-> 00:00:58,350その上、ストーリータウンビレッジもみんなにとって楽しいと思います...)

[1] => Array
    (
        [0] => 00:00:45,879
        [1] => 00:00:50,784
        [2] => 00:00:54,220
    )

[2] => Array
    (
        [0] => - Oh! Just leave me in the car with the window open a crack.
 - That's the plan
        [1] => I think it's nice we're doing something
Maggie will enjoy for once
        [2] => Besides, I'm sure Storytown Village
is also fun for everyone..
    )

)

アップデート:

foreach($matches[1] as $i => $data){
    $time = $data;
    $message = $matches[2][$i];
    mysqli_query("INSERT INTO Table (time,message) VALUES ('{$time}', '{$message}')");
}
于 2012-06-23T02:01:48.537 に答える