manipulating text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ogo796
    New Member
    • Jan 2008
    • 44

    manipulating text file

    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
  • spudse
    New Member
    • Jan 2008
    • 11

    #2
    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.

    Comment

    • ogo796
      New Member
      • Jan 2008
      • 44

      #3
      Originally posted by spudse
      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.

      Comment

      • spudse
        New Member
        • Jan 2008
        • 11

        #4
        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_conten ts() 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:
        [code=php]$line[1995][5] = "(8 June 1995) S v m [tty]xs a; 1995 (3) SA otyttt";[/code]
        Then recreate the contents of the file using foreach(). And use file_put_conten ts to write the new lines to the file.

        Comment

        Working...