SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing text in a text file

    Data from an input form is sent by email and then stored as a flatfile.
    File structure is like a csv, semicolon delimeted, eg.
    Mr;John;Doe;;Broad Street 2453;USA;80807;Chicago; etc.

    Input checking is made online - although just to a certain degree.
    Fine checking should be done offline, ie i want to read the text file, check the fields and correct them if needed, then store the result again.

    Where i'm stuck is the correction of the data 'on the fly'.
    PHP Code:
    $fh fopen("test.txt""r+");
     
    while ((
    $data fgetcsv($fh1024";")) !== false)
    {
        
    //-- Get total qty of all fields
        
    $num count($data);
     
        for (
    $i 0$i $num$i++)
        {
            
    //-- manipulate the entry and write it back
            //-- ???
     
            //-- This doesn't work, destroyes the file:
            //    trim($data[$i]);
            //    fputs($fh, $data[$i]);
     
            //-- display the single field
            
    echo $data[$i];
        }
    }
     
    fclose($fh); 
    Any idea how i can handle this?


    EDIT:
    oops, sorry, wrong forum, should be regular PHP. Please move.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can go explode(';', file_get_contents('test.txt')); and then foreach the result of that to get the different inputs. Then you can update that array and use implode to put it back together.

  3. #3
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I played with explode as well and changing dat worked, just writing the data back to the file doesn't seem to be correct.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's your code?
    PHP Code:
     <?php
     $data 
    explode(';'file_get_contents('file.txt'));
     
    $data[1] = 'nathan'// Set the second record to Nathan.
     
    file_put_contents('file.txt'implode(';'$data));
     
    ?>
    Works for me.

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that doesn't work in my case since i'm still bound to PHP v4.3.x and file_put_contents is only PHP5.x

    The original data looks like this:
    Mr;John;Doe;;Broad Street 2453;USA;80807;Chicago;
    Mrs;Jane;Doe;;BrOAder Street 2454;usa;80807;Chicago;
    Ms;Janine;Doe; lost co. ;Even Broader Street 2455 ;US ;80807;Chicago;

    Each row consists of the data of one user.
    One file might have some 100 lines.

    Pretty often the data has some 'quirks' which should be corrected.
    Simple things like trimming, upper-/lowercase etc are done online, but there will be some more validation and corrections which i want to do offline.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd write to a new temporary file and rename or copy it over an old one at the end. Otherwise, if an error happens your original file can get corrupted:

    PHP Code:
    $tmp fopen("TMP""wb");
    foreach(
    file($oldfile) as $line) {
       
    $line correct($line);
       
    fwrite($tmp"$line\n");
    }
    fclose($tmp);
    rename("TMP""oldfile"); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •