PDA

View Full Version : Couple o php questions


Mincer
12-17-2001, 03:02 PM
1) Is there an easy way to strip "'s from a text file using php?

It's not really feasible to do a s/r using a text editor as the files will be uploaded by a user and actioned directly rather than them passing through my control first.


2) How can I read a line at a time from a csv file before exploding it?

Say grab all characters into a string until \n and then process, then repeat in a loop until EOF

(I could do this in c++ - albeit a bit rusty now - but not really sure in php)

Any solutions greatly appreciated. :)

Matt.

--------------------------

If there's no way of easily reading the file a line at a time, is it a really bad idea to read a 1000+ line file in to an array in one go, and then exploding it into lines on \n, and then exploding each line at a time on the delimiter?

Lastly, if I do this, is this a good way of exploding a line at a time until EOF??



$filename = "file.txt";
$fp = fopen($filename, "r");
$file_contents = fread($fp, filesize($filename));
fclose($fp);

$lines = explode("\n", $file_contents);

// loop for as long as $lines is not EOF

while($i <= sizeof($lines)) {

// explode to get each field from line
$data = explode(";", $lines[$i]);

// do some stuff to enter fields into DB

// increment to continue looping
$i++;

}



I know that this will work ok for small files, but what would be a cut-off for 'small file' ??

Mark Hensler
12-17-2001, 03:17 PM
1) try this:
$text = str_replace("'s", '', $text);
2) try this:

$fd = fopen ("file.txt", "r");
// loop until EOF
while (!feof ($fd)) {
// read one line, up to 4KB
$buffer = fgets($fd, 4096);

// explode to get each field from line
$data = explode(";", $buffer);

// do some stuff to enter fields into DB

}
fclose ($fd);
PHP DOCS: fgets() (http://www.php.net/manual/en/function.fgets.php), fopen() (http://www.php.net/manual/en/function.fopen.php), feof() (http://www.php.net/manual/en/function.feof.php), str_replace (http://www.php.net/manual/en/function.str-replace.php)

Mincer
12-17-2001, 03:37 PM
Thanks for the quick response. :)

The first question, I needed to replace the quotation char " with nothing. I guess the principal is the same though. Can I just use \" ??

$text = str_replace("\"", '', $text);

EDIT: Yes it is. :D :D

-----------------

Secondly, just so I understand correctly... (always a bonus in my case ;))... fgets() reads one line up to endline?? (nix or win or both??)

Thanks. :)

Mincer
12-17-2001, 03:59 PM
In case anyone cares....

I added:

$fd = fopen ("data.csv", "r");

// loop until EOF
while (!feof ($fd)) {

// read one line, up to 4KB
$buffer = fgets($fd, 4096);

if ($buffer == '') exit(); // 2 single quotes
else {

// explode to get each field from line
$data = explode(";", $buffer);

$data = str_replace("\"", '', $data);

// do some stuff to enter fields into DB

}

}

fclose ($fd);

To stop it outputting garbage if there are empty lines at the end of the file. (in my case empty lines will not occur in the file up till then).

Mark Hensler
12-18-2001, 03:20 AM
fgets() will read up to the number of bytes you specify, until it reads a EOL, or until EOF (which ever comes first). PHP is wise to the win/unix differences, and will watch for \n on unix and \r\n on win for EOL.