【发布时间】:2020-12-19 15:43:07
【问题描述】:
我有一个包含以下几行的文本文件 (data.txt)
# one
# two
a-two
b-two
c-two
# three
a-three
b-three
# four
我想将 # 替换为每行递增的数字,以便像这样
1 one
2 two
a-two
b-two
c-two
3 three
a-three
b-three
4 four
我已经尝试过这样做
<?php
$path_to_file = 'data.txt';
$what_to_replace = '#';
$i = 0; // initial count
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($what_to_replace, $i++,$file_contents);
file_put_contents($path_to_file,$file_contents);
?>
它确实在所有带有# 的行中都发生了变化,但不是增量的
【问题讨论】:
-
@GiacomoM 这很有帮助,根据那里的答案,我将尝试重写代码以满足我的需求〜谢谢
-
@Reham 从欺骗目标中实现我推荐的技术:3v4l.org/VYnFH
标签: php