2012-04-21 13 views
0

我有一個文本文件,我想讀,但不包含某些字符在開始行(因此「@」,或任何一個在後面定義):PHP preg_match_all,閱讀內容和排除不必要的

@ I don't want this line to be read 
This line should be read; 
"This one" should be read, too; 
'Also this one' should be read; 
...etc 
@ But this one should be ignored; 

使用下面的代碼我可以分解以分號(「;」)結尾的代碼,但最後一行不應該,因爲它以「@」開頭。

$contents = file_get_contents($the_path); 
$result = array_map('trim', explode(";", $contents)); 

任何暗示要達到這個目的?由於

更新代碼:

// http://stackoverflow.com/questions/10257244/php-preg-match-all-read-content-and-exclude-unwanted/10257319 
    $results = array(); 
    $matches = array(); 
    $the_path = '/path/to/file.txt'; 
    if (is_file($the_path)) { 
    $contents = file_get_contents($the_path); 
    if ($contents) { 
     // ! array warning 
     // $contents = array_map('rtrim', $contents); 
     // $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT); 
     $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY); 

     if ($matches) { 
     foreach ($matches as $key => $val) { 
      $results[$key] = $val; 
     } 
     } 
    } 
    } 
    // Attempt to remove the first 0 key, and start from 1, because 0|value0 is considered NULL 
    $results = array_combine(range(1, count($results)), array_values($results)); 

    return !empty($results) ? $results : array(); 

更新2,正常工作通過DCoder:

$matches = array(); 
    if ($contents = file($the_path)) { 
     $contents = array_map('rtrim', $contents); 
     $keyword = '@'; 
     // Still output @line 
     // $matches = preg_grep('#^@#', $contents, PREG_GREP_INVERT); 
     // Ok, thanks to http://php.net/manual/de/function.preg-grep.php#85503 
     $matches = preg_grep("/{$keyword}/i", $contents, PREG_GREP_INVERT);   

     // $matches = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), NULL, PREG_SPLIT_NO_EMPTY); 
     // dsm($matches); 
     if ($matches) { 
     foreach ($matches as $key => $match) { 
     $results[$key] = $match; 
     } 
     } 
    } 


    // $results = array_combine(range(1, count($results)), array_values($results)); 
    return $results; 
+0

只是使用array_shift($ results);刪除第一項 – 2012-04-21 08:53:20

+0

謝謝。它曾被嘗試過之前結束與那個醜陋的線 – swan 2012-04-21 08:55:47

+0

謝謝,我總是接受答案,除了那些沒有答案:(。 – swan 2012-04-21 08:58:43

回答

1
// get the contents of the file as an array of lines 
$contents = file($the_path); 
if($contents === false) { 
    throw new Exception("Failed to open file {$the_path}"); 
} 
// drop ending newlines 
$contents = array_map('rtrim', $contents); 

// find all lines except those starting with @ 
$matched = preg_grep('#^@#', $contents, PREG_GREP_INVERT); 
+0

謝謝,但仍然得到數組警告,也許在我的代碼中警告:array_map()[function.array-map]:參數#2應該是一個數組 – swan 2012-04-21 08:23:01

+0

確保你試圖打開的文件確實存在(如果文件無法讀取,則返回FALSE)。 – DCoder 2012-04-21 08:25:40

+0

是的,像往常一樣用is_file($ the_path) – swan 2012-04-21 08:27:30

1

與此代碼$線將包含所有的數組不以@

開頭的行
$contents = file_get_contents($the_path); 
$lines = preg_split("/[\r\n]/", preg_replace("/@.*?[\r\n]/", "", $contents), null, PREG_SPLIT_NO_EMPTY); 
+0

幾乎可以工作,只是它增加了一個空的0 |''對預計 – swan 2012-04-21 08:24:20

+0

您能否提供無法正常工作的完整數據? – 2012-04-21 08:25:56

+0

根據需要更新。謝謝 – swan 2012-04-21 08:35:10