2014-07-02 6 views
1

J'ai un problème. J'essaie de compter le nombre de lignes de sous-titres avec php. Comme vous le savez peut-être, un sous-titre ressemble à ceci:php regex lire des lignes spécifiques

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 

Maintenant, j'ai essayé de mettre le contenu d'un fichier de sous-titres dans un tableau, comme ceci:

$f = fopen($file, 'rb'); 
$read = fread($f, filesize($file)); 
fclose($f); 
$array = explode("\n",$read); 

Avec ce code:

$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>'; 

i get:

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! 
) 

qui n'est pas correct, car

It's something designed 
to help you get healthy. 

devrait être dans un seul élément du tableau.

J'ai aussi essayé de faire correspondre tout entre (par exemple):

1 
00:00:00,984 --> 00:00:03,503 

et

2 
00:00:03,587 --> 00:00:04,821 

avec:

(\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 

mais il ne fonctionne pas et i » Je suis à court d'idées.

Ce que je suis Tring sortie:

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 

Toute aide sera appréciée.

Répondre

0

Voici une maquette:

$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>'; 

plutôt que des éléments que je ne veux Détruire pas que je suis d'ajouter les éléments que je ne veux un nouveau tableau. De cette façon, je peux fusionner des éléments de chaîne consécutifs dans un élément de mon nouveau tableau.

+0

Merci beaucoup, monsieur. Votre code fonctionne comme je l'ai demandé. – nikodemus

2

Vous pouvez utiliser le modificateur multiligne dans PCRE pour gérer les retours à la ligne incorporés après avoir lu le fichier; puis les lignes de match ne commence pas par un nombre/chiffre pour obtenir ce que vous voulez:

$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); 

Ce qui vous donne le résultat suivant:

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 
) 
+0

C'est une façon propre de le faire. – user2657915

+0

Merci, Monsieur. :) – nikodemus

0

Vous pouvez le faire comme ça, en utilisant la bibliothèque 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); 
Questions connexes