duanjiong5023 2018-02-05 20:16
浏览 13
已采纳

PHP Regex打印并计算特定列中的值[关闭]

I have a file (file.txt) that I read with PHP.

That's how it looks right now:

<?php

$file = fopen("document/test.txt", "r");

if ($file === FALSE) {
    die("Nothing found.");
}

$sum = 0;
while (($data = fgetcsv($file, 0, "\t")) !== FALSE) {
    $sum += (double) $data[11];
}
fclose($file);
echo "Total: " . $sum;

?>
  • 写回答

2条回答 默认 最新

  • dongmeiran609914 2018-02-05 21:08
    关注

    This will get you the 12th column in each line, and put's it into capture group 1.

    (?m)^(?:[^\t]*\t){11}([^\t]*)

    Formatted

     (?m)
     ^ 
     (?: [^\t]* \t ){11}
     ( [^\t]* )             # (1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?