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<=>Min nesota<*>f oo<=>fooba r (\n)
user<=>elvis2<*>state<=>Mi nnesota<*> foo<=>foob ar (\n)
user<=>elvis3<*>state<=>Mi nnesota<*> foo<=>foob ar (\n)
Given a user value I need to replace the contents of 1 line. Example....
given: user=elvis2, change
user<=>elvis2<*>state<=>Mi nnesota<*> foo<=>foob ar (\n)
to
user<=>elvis2<*>state<=>Mi nnesota<*> foo<=>kitt ywumpus (\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!
I have a text file called "data.txt". Each line contains information about a user in the following format:
user<=>elvis<*>state<=>Min
user<=>elvis2<*>state<=>Mi
user<=>elvis3<*>state<=>Mi
Given a user value I need to replace the contents of 1 line. Example....
given: user=elvis2, change
user<=>elvis2<*>state<=>Mi
to
user<=>elvis2<*>state<=>Mi
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!
ASKER
EagleEye:
Nice code :) Actually I have that part covered. It the file manipulation that I'm looking for. Thoughts?
Nice code :) Actually I have that part covered. It the file manipulation that I'm looking for. Thoughts?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice. Thanks!
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]
$statedata = explode("<=>",$datapair[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. :)