有很多方法可以做到这一点,这是我的看法...
// get lines as array from file
$lines = file('names.txt');
// get a count for the number of words on each line (PHP > 5.3)
$counts = array_map(function($line) { return count(explode('|', $line)); }, $lines);
// OR (PHP < 5.3) get a count for the number of words on each line (PHP < 5.3)
//$counts = array_map(create_function('$line', 'return count(explode("|", $line));'), $lines);
// get the sum of all counts
$count = array_sum($counts);
// putting it all together as a one liner (PHP > 5.3)...
$count = array_sum(array_map(function($line) { return count(explode('|', $line)); }, file('names.txt')));
// or (PHP < 5.3)...
// $count = array_sum(array_map(create_function('$line', 'return count(explode("|", $line));'), file('names.txt')));