我有以下代码,读取一个TXT文件,从每一行中取出不需要的信息,然后将编辑的行存储在一个新的TXT文件中。对于TXT中的每一行PHP,增加变量的值?
<?php
$file_handle = fopen("old.txt", "rb");
ob_start();
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);
foreach ($parts as $str) {
$str_parts = explode('_', $str); // Split string by _ into an array
array_pop($str_parts); // Remove last element
array_shift($str_parts); // Remove first element
echo implode('_', $str_parts)."\n"; // Put it back together (and echo newline)
}
}
$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);
fclose($file_handle);
?>
我现在想要插入$ hr #min和$ sec变量,每次保存新行时将增加1秒。比方说,我的台词这样写的(旧代码):
958588
978567
986766
我想我的新的代码看起来像这样:
125959958588
130000978567
130001986766
正如你所看到的,一小时是24小时格式(00 - 23),然后是分钟(00-59)和秒(00-59),结束时提取的txt。
我放下了可变框架,但我不知道如何让vriables正确增加。有人可以帮忙吗?
<?php
$file_handle = fopen("old.txt", "rb");
$hr = 00;
$min = 00;
$sec = 00;
ob_start();
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);
foreach ($parts as $str) {
$str_parts = explode('_', $str); // Split string by _ into an array
array_pop($str_parts); // Remove last element
array_shift($str_parts); // Remove first element
echo $hr.$min.$sec.implode('_', $str_parts)."\n"; // Put it back together (and echo newline)
}
}
$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);
fclose($file_handle);
?>
在什么时候一定时间计数器开始? – elxordi 2012-04-12 19:48:33
这个尖叫出来 - 使用数据库,而不是一个平面文件。 – 2012-04-12 19:48:50
我无法使用数据库,我必须解析文件 – Sweepster 2012-04-12 19:51:15