Jump to content

replace lines in a txt file


The Little Guy

Recommended Posts

Is it possible to replace one line in a textfile?

say I have these lines:

 

ray:house:1-21-2007_0224PM

joe:hoose:1-26-2007_0224PM

tim:hoooe:1-22-2007_0224PM

him:hoooo:1-20-2007_0224PM

 

If joe decides to log in, I need to update the time, or the 3rd "field" in the text file.

 

if I can not replace one line, what would the best way to do this?

 

P.S.

This is homework, and I cannot use a database for it.

Link to comment
Share on other sites

try:

$array = file("text.txt");
foreach ($array as $line) {
$parts = explode(":", $line);
$times[] = $parts[0];}
$linenum = array_keys($times, "joe");//joe as an example
$data = file("text.txt");
$data[$linenum] = "modified";//changing the info.
$handle = fopen($file, "w");
foreach ($data as $val) {$write = fwrite($handle, $val);}
fclose($handle);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.