I have a text file (data.txt) with the following lines
# one
# two
a-two
b-two
c-two
# three
a-three
b-three
# four
I would like to replace # with incremental number per line so to be like this
1 one
2 two
a-two
b-two
c-two
3 three
a-three
b-three
4 four
I have try to do this
<?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);
?>
it did changed in all lines with # but not incrementally