gpt4 book ai didi

php - 用每行的增量数字替换特定行的字符

转载 作者:行者123 更新时间:2023-12-05 09:07:02 24 4
gpt4 key购买 nike

<分区>

我有一个包含以下行的文本文件 (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);
?>

它确实在所有行中用 # 改变了,但不是递增的

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com