2010-01-06 72 views

回答

0

的一切,如果你有新的生產線的每個字首先然後你行第一:

$contents = file_get_contents ($path_to_file); 
$lines = explode("\n", $contents); 
if (!empty($lines)) { 
    foreach($lines as $line) { 
     // Then you get rid of sequence 
     $word_line = preg_replace("/^([0-9]\))/Ui", "", $x); 
     $words = explode(" ", $word_line); 
    } 
} 

(假設序列開頭「X)」)

3

假設你的文件是這樣的:

1)First 2)Second 3)Third 4)Forth 
5)Fifth 6)Sixth .. 

使用此功能,您可以只提取了一句話:

preg_match_all('/[0-9]+\)(\w+)/', $file_data, $matches); 

現在$matches[1]將包含:

Array 
    (
     [0] => First 
     [1] => Second 
     [2] => Third 
     [3] => Fourth 
     [4] => Fifth 
     [6] => Sixth 
    ) 
+0

輝煌的解決方案。 –

+0

另一個可能的正則表達式是#(?<= \ b)([A-Za-z] +)(?= \ b)#。無論周圍的數字,括號等如何都匹配所有單詞 – selfawaresoup

0

假設文件的內容就像duckyflip說明什麼,另一種可能的方式

$content = file_get_contents("file"); 
$s = preg_split("/\d+\)|\n/",$content); 
print_r(array_filter($s)); 

輸出

$ php test.php 
Array 
(
    [1] => First 
    [2] => Second 
    [3] => Third 
    [4] => Forth 
    [6] => Fifth 
    [7] => Sixth 
) 
相關問題