[Solved] Simply edit custom line from .txt with PHP [closed]


Next code is one way to do it. Pay attention to the comments in the code :

<?php
$line = 2; // LINE TO REPLACE.
$my_array = file( "my_file.txt" ); // GET WHOLE FILE AS ARRAY OF STRINGS.
$my_array[ $line ] = "abc" . "\n"; // REPLACE THIRD LINE (2). NOTICE THE "\N".
file_put_contents( "my_file.txt",             // SAVE ARRAY IN FILE.
                   implode( "",$my_array ) ); // CONVERT ARRAY INTO A BIG STRING.
?>

This is the idea : read the entire file as an array (file), now you can replace the desired line by its index, then you convert the array as string (implode), and save it to file (file_put_contents).

2

solved Simply edit custom line from .txt with PHP [closed]