2013-06-25 31 views
-4

線陣列我有了這樣的內容線陣列:你如何解析在PHP

5 1 0 49972729 1813694  7 218  0  0  0  0 747 16811 13911 1 1 96 1 0.53 3.3 12:51:44 
6 1 0 49976522 1806247  3 203  0  0  0  0 613 41885 13113 3 2 95 1 0.86 5.4 12:52:14 
    kthr   memory       page      faults     cpu    time 
---------- --------------------- ------------------------------------ ------------------ ----------------------- -------- 
r b p  avm  fre fi fo pi po fr  sr in  sy cs us sy id wa pc ec hr mi se 
8 1 0 49979036 1800213  1 190  0  0  0  0 650 54141 14509 2 2 95 1 0.80 5.0 12:52:44 
6 1 0 49981360 1807204  2 235  0  0  0  0 641 31630 18005 2 1 96 1 0.70 4.4 12:53:14 

我想去,雖然每行和每一個以數字開頭的行推送到NE陣列。

我有這個至今:

{for ($i=0; $i<sizeof($line); $i++) 
if(preg_match("^[0-9]", $line[i]) 
    array_push($line[$i], $new_line); 

} 

我得到這個錯誤:

Parse error: syntax error, unexpected T_STRING in C:\PHP\cpu.php on line 20 

我非常新的PHP和真正欣賞任何見解。

+4

我看到這樣的問題太多了。花時間學習如何[解釋錯誤並修復代碼](http://jason.pureconcepts.net/2013/05/fixing-php-errors/)。 –

+0

您在'if'結尾處缺少一個')'來啓動。 – complex857

+0

第20行在哪裏? – Joni

回答

1
$rawLines = explode("\n", $content); 
$lines = array(); 
foreach($rawLines as $rawLine){ 
    $rawLine = trim($rawLine); 
    $parts = explode(" ", $rawLine); //maybe explode("\t", $rawLine); 
    $parts[0] = trim($parts[0]); 
    if(!ctype_digit($parts[0])){ 
     continue; 
    } 
    $lines[] = $parts; 
} 
+0

最後一行$ lines [],包含我所有的行?我將如何打印它?我嘗試echo $ lines [0],它給了我輸出爲「數組」 – user1471980

+0

我最終需要做的是將最後一個$ lines數組插入到db表中。 – user1471980

+0

$ lines包含你的行數組,數組中的每個條目都是你行中數字的數組..要將它打印爲字符串do:foreach($ lines as $ line){echo implode(「」,$線),「
」; }在代碼中的foreach之後.. – Johny

0

當你修復您的腳本語法錯誤(缺少)爲例),您將需要分隔符的您正則表達式:

if(preg_match("/^[0-9]/", $line[$i])) 
      ^here^