tags:

views:

235

answers:

1

hey all i'm trying to make a site where people paste a CSV file directly into a textarea (say its called "original-text") and parse it, with my script then generating something from that. i know how to parse a csv file on the server (using fgetcsv) but not how to get the input from a POST variable INTO that fgetcsv. so far i've tried something like this, to no avail:

$file_handle = fopen("php://input", "r");

while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle);
    echo $line_of_text[0] . '<br />';
}
+4  A: 

fgetcsv() has a cousin named str_getcsv() that should do exactly what you need. As so often, wonderfully named (once without, once with underscore). Ah well! But we still love PHP, don't we.

Example (with no sanitation!!! I can't see any necessary at this point, though.)

$array_csv = str_getcsv($_POST["csv"]);
Pekka
Thanks Pekka! small thing though - my host is only running PHP 5.2.x, which doesnt support this function... is there another easy way to do something like this, short of asking them to upgrade?
yonation
@yonation Ah, I didn't see that it's >= 5.3.0. Check out the user comments in the manual page, there are a number of replacement functions. I'm sure you'll find one that fits.
Pekka