SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to learn how to use PHP to manipulate data files.. is there a good tutorial for this? I've read over quite a few, but I'm now more confused than ever!

    Here's what I want to do:

    Code:
    The data file looks like this:
    jimbob:asdfnasdfnfnsdf
    nobody:asdfjdfsi3sf
    yourname:asd99sdf9sf9fd
    I need to extract the data after the colon for whatever username I am given. (jimbob, nobody, yourname).

    Then, I need to change this data and stick it back into the file.

    I'm sure this is easy to do.. but I've spent an hour on this and still haven't figured out how to do it. I'm fscan, fread, fgetss, and fgets'ed right out!

    Thanks folks.
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular Expression!!!!

    After reading the file to a variable, use RE.
    Also, the format of a file decides what you have to do with the data (like split it out with "\n" or ":", etc)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have a link to a tutorial that explains how to do this?

    Thanks again.
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  4. #4
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will read the contents in, and split up your data based on the : separator.

    Code:
    $fcontents = file ('test.txt');
    
    while (list ($line_num, $line) = each ($fcontents)) {
       $parts = split(":", $line);
       echo "<b>Line $line_num:</b><br>\n<li>Login: $parts[0], Pass: $parts[1]<br>\n";
    }

  5. #5
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will do the same as the previous post, but it will also look for a certain login while reading the file, and will update the contents. There should be a better way of deleting the contents of the temp file, but I'm not really seeing anything right now

    Code:
    $fcontents = file ('test.txt');
    $tempfile = fopen("tempfile.txt",'w+');
    
    while (list ($line_num, $line) = each ($fcontents)) {
       $parts = split(":", $line);
       echo "<b>Line $line_num:</b><br>\n<li>Login: $parts[0], Pass: $parts[1]<br>\n";
       
       if ($parts[0] == "nobody") {  
          $line = "nobody:newpass\n";
       }
       
       fwrite($tempfile, $line); // write out contents, possibly modified
    } 
    
    fclose($tempfile); 
    copy("tempfile.txt","test.txt");    
    $tempfile = fopen("tempfile.txt",'w');  // erase contents..
    fwrite($tempfile,"");
    fclose($tempfile);


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
  •