By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

manipulating text file

P: 44
hi there! i have a problem manipulating a text file using php. I want to write information posted from an html form. from the example below i want to upload the file and it must be under the specific year and the number must be i sequence. i want the php script to check the year and the number and post the new file under the specific year and number .eg if i upload file 1995_5.rtf it must be written under 1995_4.rtf.


1995_1rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1995_2.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1995_3.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1995_4.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1996_1.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1996_2.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1996_3.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1996_4.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1997_1.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1997_2.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1997_3.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
1997_4.rtf (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
Jan 7 '08 #1
Share this Question
Share on Google+
3 Replies


P: 11
A small database would be perfect for this. SQLite comes with PHP5 and is quite easy to work with and quick to setup.

Are you experienced with databases? If not, it's not as hard as it seems.
Jan 7 '08 #2

P: 44
A small database would be perfect for this. SQLite comes with PHP5 and is quite easy to work with and quick to setup.

Are you experienced with databases? If not, it's not as hard as it seems.

hi,thanks for you'r advice but i am not using the database,i read and write the data to and from the text fie i am using flat file.
Jan 8 '08 #3

P: 11
I think the easiest way is to make an assiciative array of all lines in the file. First you have to get the files in a variable, file_get_contents() is usefull for this. Then use a regular expression function to get all lines in an array, preg_match_all for instance.

$line [1995] [1] = (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
$line [1995] [2] = (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt
...
$line [1997] [4] = (8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt

And add the new line to the file, with:
Expand|Select|Wrap|Line Numbers
  1. $line[1995][5] = "(8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt";
Then recreate the contents of the file using foreach(). And use file_put_contents to write the new lines to the file.
Jan 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.