Link to home
Start Free TrialLog in
Avatar of djlurch
djlurch

asked on

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!
Avatar of EagleEye1975
EagleEye1975

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. :)
Avatar of djlurch

ASKER

EagleEye:

Nice code :)  Actually I have that part covered.  It the file manipulation that I'm looking for.  Thoughts?
ASKER CERTIFIED SOLUTION
Avatar of EagleEye1975
EagleEye1975

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of djlurch

ASKER

Nice.  Thanks!