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;

?>

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)

These are actually tab delimited values, a "dialect" of csv. PHP comes with csv support:

$fd = fopen("filename", "r");
if ($fd === FALSE) {
    die("Failed to open file");
}
$sum = 0;
while (($data = fgetcsv($fd, 0, "\t")) !== FALSE) {
    $sum += (double) $data[11];
}
fclose($fd);
echo $sum;