Amin Maleki
Amin Maleki

Reputation: 156

Modify Line n With PHP?

I want to write on line 5 of my file.

I can't use replace because my file use many numbers in lines. So I need to know how can modify line 5.

You know we can read line 5 of file with:

$File='myfile.txt';

$readline = file($File);

$line5=$readline[4];'

I need to code that can modify only line 5 of $File.

Upvotes: 0

Views: 76

Answers (1)

meda
meda

Reputation: 45500

In your code $read is undefined. It should be $readline, the following should work for you:

$File = 'myfile.txt';
$lines = file($File);
$lines[4] = 'modified line';
file_put_contents($File, implode("\r\n", $lines));

Upvotes: 2

Related Questions