reading/writing a file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dot Kom

    reading/writing a file?

    I've got this text file full of lines like:

    A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î

    the columns are defined by the *#, so that line would break apart
    like:
    A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234Î


    Basically, what I need to do is read this file and I need to write
    field 1, 4 and 5 which need to go into a text file in tab delimited
    format like:

    King Richard Dale Petersburg 257-1234

    What can I do to get this task accomplished? I'm familiar with
    requesting and manipulating data from mysql but this reading/writing
    to a txt file is completely new to me. I have about 1 years worth of
    off and on PHP experience. Any help is greatly appreciated. Thank you

    Jason
  • Douglas Abernathy

    #2
    Re: reading/writing a file?


    "Dot Kom" <jrcooke@hardyn et.com> wrote in message
    news:5c9ba18d.0 310201107.72729 7b0@posting.goo gle.com...[color=blue]
    > I've got this text file full of lines like:
    >
    > A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î
    >
    > the columns are defined by the *#, so that line would break apart
    > like:
    > A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234Î
    >
    >
    > Basically, what I need to do is read this file and I need to write
    > field 1, 4 and 5 which need to go into a text file in tab delimited
    > format like:
    >
    > King Richard Dale Petersburg 257-1234
    >
    > What can I do to get this task accomplished? I'm familiar with
    > requesting and manipulating data from mysql but this reading/writing
    > to a txt file is completely new to me. I have about 1 years worth of
    > off and on PHP experience. Any help is greatly appreciated. Thank you
    >
    > Jason[/color]

    This can be solved in a few steps, but you'll need to read up on the
    associated functions yourself.

    Step 1: If the file is not too large, you can load it into an array variable
    with the "file" function:

    $array_data = file("/www/htdocs/data/thefile.dat");

    Step 2: You can slice up each line of the file at the delimeters with the
    "explode" function:

    foreach ($array_data as $array_line) {
    $array_elements = explode("*",$ar ray_line);
    // $array_elements[0] would have "A"
    // $array_elements[1] would have "1King Richard Dale", etc.
    // Process items here; remember to extract the leading number character(s)
    }


    Comment

    • Nikolai Chuvakhin

      #3
      Re: reading/writing a file?

      jrcooke@hardyne t.com (Dot Kom) wrote in message
      news:<5c9ba18d. 0310201107.7272 97b0@posting.go ogle.com>...[color=blue]
      >
      > I've got this text file full of lines like:
      >
      > A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î[/color]

      Wow... A weird setup, isn't it?
      [color=blue]
      > Basically, what I need to do is read this file and I need to write
      > field 1, 4 and 5 which need to go into a text file in tab delimited
      > format like:
      >
      > King Richard Dale Petersburg 257-1234
      >
      > What can I do to get this task accomplished?[/color]

      Something like this:

      $in = fopen ('your_old_file .txt', 'r');
      $out = fopen ('your_new_file .txt', 'w');
      if (!$out) {
      die ('Cannot open the output file for writing. ');
      }
      if (!$in){
      die ('Cannot open the input file for reading. ');
      }
      while (! feof ($in)) {
      $items = explode ('*', fgets ($in, 4096));
      $line = '';
      foreach ($items as $item) {
      $num = $items{0};
      if (($num == 1) or ($num == 4)) {
      $line .= substr ($item, 1) . "\t";
      }
      if ($num == 5) {
      $line .= substr ($item, 1) . "\r\n";
      }
      }
      if ($line <> '') {
      fputs ($out, $line);
      }
      }
      fclose ($in);
      fclose ($out);

      Cheers,
      NC

      Comment

      Working...