Ask for file handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lion cave
    New Member
    • Apr 2009
    • 23

    Ask for file handling

    Hi gud day,

    I would ask on how to insert data between the data on file.

    Example Data contain inside on the file look like this sample.txt:
    =============== =============== ===
    sample data line 1 : the id
    sample data line 2: text
    sample data line 3: the id
    sample data line 4: text

    =============== =============== ===

    I want to insert the data "data inserted" inside between the "sample data line 2: text" and "sample data line 3 : the id".

    Note that the id is same as a primary key and the text is the data under the id.

    Output would would look like this.
    =============== =============== ===
    sample data line 1 : the id
    sample data line 2: text
    data inserted
    sample data line 3: the id
    sample data line 4: text

    =============== =============== ===

    Is there anyone who know a best php function that suite for this and has a effecient running time?

    Any suggestions is highly appreaciated. Thanks
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I suggest reading the file using file().

    This returns an array with one line per element.
    Then loop through the array at the same time concatenate lines to string
    or in another array
    But when an id is encountered (I am assuming they have an identifiable structure)
    insert your date before the id.

    Then use file_put_conten ts() to the new file

    Comment

    Working...