【发布时间】:2015-09-06 10:41:21
【问题描述】:
我有包含以下数据的 test.txt
2015-06-19 14:46:10 10
2015-06-19 14:46:11 20
2015-06-19 14:46:12 30
(这是自动生成的,无法编辑) 然后我使用以下 php 脚本写入名为 tempdata.txt 的临时文件
<?php
$myFile = "filelocation/test.txt";
$myTempFile = "filelocation/tempdata.txt";
$string = file_get_contents($myFile, "r");
$string = preg_replace('/\t+/', '|', $string);
$fh = fopen($myTempFile, 'w') or die("Could not open: " . mysql_error());
fwrite($fh, $string);
fclose($fh);
?>
这使得 tempdata.txt 看起来像:
2015-06-19 14:46:10|10
2015-06-19 14:46:11|20
2015-06-19 14:46:12|30
但是我想在每行的开头添加行号,如下所示:
1|2015-06-19 14:46:10|10
2|2015-06-19 14:46:11|20
3|2015-06-19 14:46:12|30
有什么方法可以读取 php 中的行号并将其添加到每一行的前面,如“n|” ?
【问题讨论】: