Replace 1 Line in A Text File with PHP

Greetings fellow ee'ers:

I have a text file called "data.txt".  Each line contains information about a user in the following format:

user<=>elvis<*>state<=>Minnesota<*>foo<=>foobar   (\n)
user<=>elvis2<*>state<=>Minnesota<*>foo<=>foobar   (\n)
user<=>elvis3<*>state<=>Minnesota<*>foo<=>foobar   (\n)

Given a user value I need to replace the contents of 1 line.  Example....

given: user=elvis2, change

user<=>elvis2<*>state<=>Minnesota<*>foo<=>foobar   (\n)
to
user<=>elvis2<*>state<=>Minnesota<*>foo<=>kittywumpus (\n)

Options:
#1) Read text file into a string, save as data2.txt then rename
#2) Delete the appropriate line (?) and append the file
#3) Clear the file (?) and write the string
#4) ????


Thanks!
LVL 1
djlurchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EagleEye1975Commented:
I would read it in to an array of strings, one string per line.

Then I would edit the string to reflect the changes, probably by rebuilding the string...

$file = "/path/to/file/data.txt";
$fileline = file($file);
$newfile = array();
$newuserdata = array();
foreach ($fileline as $thisline)
{
   $datapair = explode("<*>",$thisline); // Now each data pair is in an array.
   $userdata = explode("<=>",$datapair[0]); // Get the username as $userdata[1]
   $statedata = explode("<=>",$datapair[1]); // get the state as $statedata[1]
   //   ... and so on for all data pairs  //
   if ($userdata[1] == $desireduser)  // We have the user we want...
   {
      $userdata[1] = $newuserdata;
      $statedata[1] = $newstate;
      $otherdata[1] = $newotherdata;
      // ... rewrite values for all data pairs //
      $newuserdata[0] = implode("<=>",$userdata);
      $newuserdata[1] = implode("<=>",$statedata);
      $newuserdata[2] = implode("<=>",$otherdata);
      // ... re-implode the info into the data pairs again. //
      $thisline = implode("<*>",$newuserdata);
      // We're back to having "thisline", but now it contains the new data.
   }
   // No else line, because others are not having changes made.  
   $newfile[$i] = $thisline;
   $i += 1;
}

You can write the file out as the file is being read, or you can read it all in to an array (as in my example) and then itterate through the array and write each line.  There may be a function to write ALL lines in an array to a file too... check php.net for that kind of thing.

But you see what I did... I broke up file line by line.  I broke up the lines in to data pairs.  I checked to see if this line's user info matched the desired user we're changing... if so, I rewrote all of the VALUES in the data pairs to equal the new values needed... then I reconstructed the pairs from the parts, reconstructed the line from the pairs... and put the line back in to the file. :)

Basically, ripped it apart, and put it back together again. :)
0
djlurchAuthor Commented:
EagleEye:

Nice code :)  Actually I have that part covered.  It the file manipulation that I'm looking for.  Thoughts?
0
EagleEye1975Commented:
I'd just open a new file, overwriting the old one (instead of appending) and rewrite all of the lines.

Or... alternatively... as you go through data.txt line by line, write to newdata.txt line by line after any changes are made (writing each line as you're still looping)... then when you're done, unlink("data.txt") and do a move of newdata.txt to data.txt. :)

If you really want to be snazzy, you can make a backup of data.txt to data.bak (overwriting any existing data.bak) and THEN doing a file COPY of newdata.txt to data.txt. :)

For info on outputting to a file:

http://us2.php.net/manual/en/function.file-put-contents.php

Or you can use:

http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/manual/en/function.fwrite.php
http://us2.php.net/manual/en/function.fclose.php

That may be the preferred way...

Open the file

Loop while reading from one file, do the fwrite to the new file

Once done looping, close the new file.

Make sure you preserve \r\n and such...

Then see
http://us2.php.net/manual/en/function.copy.php
for info on how to copy files. :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
djlurchAuthor Commented:
Nice.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.