Jump to content

Replacing a line with another line


plzhelp

Recommended Posts

hi,i have a text file(1.txt) like...line1................................line2................................line3................................line4................................line5.........................................................................and so onnow i want to replace any line suppose LINE 3 with some other line in some other text file(2.txt, that line in 2.txt is only one line in that with some modifications)like..LINE 3.............(in 2.txt)i want to replace that.how can i do that???-plz help

Link to comment
Share on other sites

There may be a better way, but this is how I do it....Read your first file into an array. Then overwrite the original file while inserting the new line you want.

<?$linetoinsert=3;//Get new line$z=file("2.txt");///Put original file into array$old=file("1.txt");//Rewrite the file$x=fopen("1.txt", "w");$old[$linetoinsert]=$z;foreach($old as $value){  fwrite($x, $value);}fclose($x);?>

Link to comment
Share on other sites

If you want to create a database with plain text files, my suggestion is to work with multiple files as they were rows in a table instead of working with multiple lines in a few files. I had this experience and found easier and more flexible.You could write to the files using variables like this:$write_this = "\$string = \"this is my string\"";And then you could use include() to display the stored variables...Use also arrays (array, array_push, etc) to gather selected files (which play rows)...And to complete this approach, the "id" of the files could be based on time stamps (year.month.day.hour.minutes.seconds), so that you can easily sort them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...