Loop an fgets()

i.e.
$fp = fopen($file)
while ($filerow = fgets($fp,1024)) /* Read 1024 bytes or to EOL, whichever
is first) */
{
     $filerow = str_replace("; ","", $filerow);
     $filerow = str_replace("# ","", $filerow);
     $filerow = str_replace("\r","", $filerow);
     $size = strlen($filerow);
     $row_split = preg_split("/\s+/", $filerow);

     $value1 = $row_split[0];
     $value2 = $row_split[1];
     $value3 = $row_split[2];
}

Matt
----- Original Message -----
From: Lee P. Reilly <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, November 26, 2002 2:58 PM
Subject: [PHP] Upload problem - PC, *nix, and Max EOL characters


> Hi everyone,
>
> I have an application, where users can upload data files formatted as
> follows:
>
>     0.0106375           686.601            65.391
>     0.01147           606.467            54.997
>     0.0122815           640.284           46.5355
>     0.0130753           668.396           39.3671
>     0.0138809            690.05           34.6031
>     0.0146936           619.947           30.5329
>     0.0155071           640.413           26.9145
>
>
> My code deals with the uploaded files by splitting the lines into an
> array, and then obtaining the 3 particular fields from the array
> elements. My code is something like this:
>
> -=-= BEGIN CODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> // put each line of the file into an array
> $filearray = file($file);
>
> // parse the file one line... at... a... time
> while (list(,$oneline) = each($filearray))
> {
>   // if the line starts with ";" or a "#"...
>   if ($oneline[0]==";" || $oneline[0]=="#")
>   {
>     // take out the ";" and EOL characters
>     $oneline = str_replace("; ","", $oneline);
>     $oneline = str_replace("# ","", $oneline);
>     $oneline = str_replace("\r","", $oneline);
>     $size = strlen($oneline);
>   }
>
>   /* Some IQ files seperated columns by tabs; others, by whitespace.
>      We parse the data file are delimit the columns by 'X9728430182298Z'
>      (a random string), which will allow the program to recognise the
> start
>      and end of each column.
>
>     e.g.
>     0.00542           8691.64           901.605
>     0.006775            9381.3           1235.78 becomes...
>
>     0.00542X9728430182298Z8691.64X9728430182298Z901.605
>     0.006775X9728430182298Z9381.3X9728430182298Z1235.78
>   */
>
> $oneline = ltrim($oneline);
> $oneline = preg_replace("/\s+/", "X9728430182298Z", $oneline);
> $text = explode ("X9728430182298Z", $oneline); // now element [0] = Q,
> [1] = I. [2] = Error
>
> -=-= END CODE =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> This works perfectly well when the data is uploaded from a PC, but it
> fails when it's uploaded from Mac/Unix. When I vied the uploaded file
> the EOL characters *appear* as "
> ", which I believe is actually "\n". When I output the contents of
> $filearray[0] (uploaded via a PC) I get one line, but when I ouput
> $filearray[0] from a file uploaded via a Mac I get the entire file.
> Therefore the problem occurs with:
>
> $filearray = file($file);
> while (list(,$oneline) = each($filearray))
>
> I thought I had found a solution to the problem on the web* where I
> found the following extract of php.ini file:
>
> --------------------------
> ; If your scripts have to deal with files from Macintosh systems,
> ; or you are running on a Mac and need to deal with files from
> ; unix or win32 systems, setting this flag will cause PHP to
> ; automatically detect the EOL character in those files so that
> ; fgets() and file() will work regardless of the source of the file.
> ; auto_detect_line_endings = Off
> --------------------------
>
> However, this never solved the problem.
>
> The file itself is stored in a database, so how can I get rid of the EOL
> characters before it is being inserted? Is there a function to change
> the Unix EOL chars to PC EOL chars?
>
> Thanks in advance.
>
> - Best regards,
>    Lee Reilly
>
>
*[http://216.239.33.100/search?q=cache:GwerlDVFv8wC:linux.sarang.net/ftp/mir
ror/development/language/php_cvs/php4/php.ini-dist+MAC+EOL+character+PHP&hl=
en&ie=UTF-8]
>
> --
> Lee P. Reilly,                                          ms:G758
> Szilard Resource,                              tel:505-665-7025
> Bioscience Division,
> SM-30 Bikini Atoll Rd,
> Los Alamos National Laboratory,         mailto:[EMAIL PROTECTED]
> Los Alamos, NM 87545.              http://home.lanl.gov/lreilly
>
>           "Quidquid latine dictum sit, altum viditur"
>
> -._    _.--'"`'--._    _.--'"`'--._    _.--'"`'--._    _
>     '-:`.'|`|"':-.  '-:`.'|`|"':-.  '-:`.'|`|"':-.  '.` : '.
>   '.  '.  | |  | |'.  '.  | |  | |'.  '.  | |  | |'.  '.:   '.  '.
>   : '.  '.| |  | |  '.  '.| |  | |  '.  '.| |  | |  '.  '.  : '.  `.
>   '   '.  `.:_ | :_.' '.  `.:_ | :_.' '.  `.:_ | :_.' '.  `.'   `.
>          `-..,..-'       `-..,..-'       `-..,..-'       `         `
>
> --
> 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

Reply via email to