php txt 每行,PHP中每行TXT,增加变量值?

我读取TXT文件的代码如下,从每行中取出不需要的信息,然后将编辑的行存储在新的TXT文件中。

$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正确地增加。有人可以帮忙吗?

$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);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值