文本文件特定列的总和PHP

时间:2013-09-05 06:07:38

标签: php text-files calculated-columns

我有一个用数字行填充的txt文件...我有一个代码,其中用户可以过滤txt文件中的特定行

过滤代码:

<?php
$file = 'upload/filter.txt';
$searchfor = $_POST['search'];
$btn = $_POST['button'];
$sum = 0;

if($btn == 'search') {

//prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
 $contents = file_get_contents($file);

// escape special characters in the query
 $pattern = preg_quote($searchfor, '/');

// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";


// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
 }
else{
echo "No matches found";
 }

 }
 ?>

例如,过滤结果现在将从文本文件

中产生1,000,000行中的4行

0100002291310106200140001020055460000000001000000072860103508104 0100002291320106200140001020055460000000005000000072860103508101 0100002291330106200140001020055460000000001000000072860103508102 0100002291340106200140001020055460000000005000000072860103508109

我需要一个只选择特定列的代码,并使用PHP代码将它们添加到一起...例如我只想添加1-10列,所以结构将从上面给出的行看起来像这样

sructure:

0100002291
0100002291
0100002291

请帮助

0 个答案:

没有答案