2012-06-23 78 views
1

我的SRT文件的目錄,格式化:如何使用正則表達式將srt文件插入到mysql中?

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%確定正則表達式是要走的路嗎?

+0

歡迎來到StackOverflow。你到目前爲止嘗試過哪些方法無效? –

+0

謝謝Ken,我不是一個編碼員(我可以操作基本的東西,但沒有更多),所以我一直在做一些我不完全瞭解的東西的谷歌複製粘貼。這完全逃脫了我 - 太多移動部件,我拼命拼湊在一起。所以我承認失敗並尋求幫助:) – Justin

回答

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 我認爲我們很高興我們正在做點什麼 Maggie會享受一次。 [2] => 00:00:54220 - > 00:00:58350 此外,我敢肯定,Storytown村 也是大家的樂趣... )

[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}')"); 
} 
+0

感謝sixeightzero,我將如何將數組從MySQL數據庫中取出?我知道這是一個非常簡單的問題,但我是一個相當低級別的人,這是我無法弄清楚的難題之一。 – Justin

+0

已更新的答案。以上應該工作,只需更改列和表名稱。 –

+0

感謝六,由於某種原因,雖然我的輸出只顯示'Array([0] => Array()[1] => Array()[2] => Array())'我在做什麼錯了? – Justin

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"; 
} 

the demo,看看哪些變量被分配到從文件中的值。

+0

謝謝尼克!如果我想讀取文件(或文件目錄)而不是粘貼文本,我會用'$ filename ='file.txt'替換'$ str ='嗎? $ fp = fopen($ filename,「r」)' – Justin

+0

不,你應該把'$ filename ='file.txt'; $ str = file_get_contents($ filename);'$ lines = ...' – nickb

+0

之前再次感謝Nick,我試了一下,發現有關偏移的錯誤。我認爲這是因爲一些文件在時間戳之後只有一行文本。我想我需要改變'$ lines [$ i + 1]);'但是我不確定如何定義變量「它可能是1,2或3行....」只需查找空白線」。我在想這可能會使字符串操作更難?對不起,如果這是一個明顯的答案 – Justin

相關問題