2013-06-25 27 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^