Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Replace 1 Line in A Text File with PHP

Posted on 2004-11-06
4
Medium Priority
?
247 Views
Last Modified: 2006-11-17
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!
0
Comment
Question by:djlurch
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:EagleEye1975
ID: 12515838
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
 
LVL 1

Author Comment

by:djlurch
ID: 12515850
EagleEye:

Nice code :)  Actually I have that part covered.  It the file manipulation that I'm looking for.  Thoughts?
0
 
LVL 2

Accepted Solution

by:
EagleEye1975 earned 1200 total points
ID: 12515937
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
 
LVL 1

Author Comment

by:djlurch
ID: 12515946
Nice.  Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question