Re: [PHP] Simple reading a file and extract fields
Ronald Wiplinger wrote: > I got a larger file which consists of lines with a defined length of 56 how large? for very big files it's dangerous to read them in all at once, as was exampled in other replies. I would say that large in this case would be anything bigger than 5 Mb (this is a very rough estimate, we could have a whole thread devoted to this [issue]) > characters. Each line ends with a line feed (0A), > > From each line I want one field from position 1 ~ 5 and field 2 from > position 7 ~ 46. > your uisng php5 I hope? if so (error checking code left as an exercise to the reader): low flying tip: http://php.net/ http://php.net/foreach http://php.net/array_filter http://php.net/explode http://php.net/file_get_contents http://php.net/substr RTFM is a mantra not an insult! > I tried: > > $myFile = "plaiso"; > $fh = fopen($myFile, 'r'); > $theDataLine = explode("\n",fgets($fh)); > > echo "Field1Field2"; // headline > > for($i=0;$i > $Field1 = substr($theDataLine[$i], 0, 5); > $Field2 = substr($theDataLine[$i], 7, 46); > echo "$Field1 $Field2"; > } > > It prints only the headline and the first record. > > What do I miss? > > bye > > Ronald > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple reading a file and extract fields
The fgets() function only reads a single line from the file. See the below link for a way to read through all lines in the file. Then after reading each line, use substr to pull out the fields. http://us.php.net/manual/en/function.fgets.php On Nov 13, 2007 8:53 AM, Ronald Wiplinger <[EMAIL PROTECTED]> wrote: > I got a larger file which consists of lines with a defined length of 56 > characters. Each line ends with a line feed (0A), > > From each line I want one field from position 1 ~ 5 and field 2 from > position 7 ~ 46. > > I tried: > > $myFile = "plaiso"; > $fh = fopen($myFile, 'r'); > $theDataLine = explode("\n",fgets($fh)); > > echo "Field1Field2"; // headline > > for($i=0;$i >$Field1 = substr($theDataLine[$i], 0, 5); >$Field2 = substr($theDataLine[$i], 7, 46); >echo "$Field1 $Field2"; > } > > It prints only the headline and the first record. > > What do I miss? > > bye > > Ronald > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php