Hi all,

I've got a datafile that i need to upload into a mysql db. The data looks something like this, aka a chunk:

1|10/1/2004 10:23:08|"NAME: SomeAlert
DESC: Some description here.
and more description
some continued data here
and lastly continued here.
"

and there are something like 2000 of these "chunks"

The first digit before the first pipe, 1 in this case (but goes up to > 2000|), I'm not concerned about data wise.

I was hoping that each of those chunks was 1 line...but they are not. There are new lines for each line. What I really wanted to be able to do was to split or explode like:

$theFile = file('somefile.txt');
foreach ($theFile as $line) {
    list($num, $dateRaw, $alert) = split ('\|', $line)
}

but that won't work because not each line has the pipe.

I've got a work around to grab the first two chunks, but not the 3rd chunk...basically everything after the 2nd pipe on the first line. that 3rd chunk needs to remain intact (with the returns, or newlines) for each row entry into the db.
This "format" is consistent throughout the data file, but the alert, or third chunk varies in length greatly (but does have a starting and ending double quote).

Any ideas on how I can grab that 3rd chunk in it's entirety?

Thanks!

    preg_match_all('/(\d+)\|(\d{1,2}\/\d{1,2}\/\d{4}) (\d{1,2}:\d{1,2}:\d{1,2})\|"([^"]+)"/s', $str, $arr);
    

    $arr[1][] contains the first numbers the 1-2000 part
    $arr[2][] contains the date mm/dd/yy
    $arr[3][] contains the time hh:mm:ss
    $arr[4][] contains the data from inside the " marks

    so
    echo $arr[4][0];
    will display the text block from the first entry
    echo $arr[4][1];
    will display the text block from the second entry etc.

      wow...very interesting. thanks for the reply Drew!

      however, when i run that....i get a parse error because $str (or $theFile in my case) is an array...not a string.

      I'm thinking i can't do this preg_match_all within a foreach loop becuase within each chunk are the multiple new lines. So how should this be utilized?

        If $theFile was read using [man]file[/man] it would be an array of lines, but if it was read using [man]file_get_contents[/man] it would be a string.
        Or of course (as was usual before file_get_contents() was implemented) you could turn the array into a string using [man]join[/man].

          ok...you guys have reaffirmed my belief that while I've coded in php for a while now, there is always something new to learn 😃

          thanks for the suggestion Weedpacket!

          what I had done in the interim was to

          $bigString = "";
          $theFile = file('somefile.txt');
          
          foreach ($theFile as $line) {
              $bigString .= $line;
          }
          
          preg_match_all('/(\d+)\|(\d{1,2}\/\d{1,2}\/\d{4}) (\d{1,2}:\d{1,2}:\d{1,2})\|"([^"]+)"/s', $bigString, $arr);
          

          im not sure if using the foreach like that to create the string is the best way or not, but it seems to work for now 😉

          thanks for expanding my knowledge guys!

            eh save yourself a few lines and a tad bit of speed and try
            $bigString = file_get_contents('somefile.txt');

            never hurts to cut a few lines out 😉

              yup...i had only used the foreach solution before i saw Weedpackets' and your suggestion. i'm now using file_get_contents.

              thanks again.

                Write a Reply...