Help Needed with CSV & PHP

From: Date: Tue, 04 Jul 2000 20:22:27 +0000
Subject: Help Needed with CSV & PHP
Groups: php.general 
Request: Send a blank email to php-general+get-4808@lists.php.net to get a copy of this message
Hello, I would like to know how to best seperate fields of human input using PHP. I was using strtok: $tok = strtok($dataarray[$viewcard-1],":"); but it was clearly pointed out that any user that inputed data using the a colon would screw it up. I was going to try to switch over to using Comma Seperated Values (CSV) in PHP, with something like this: while ($dataarray[$viewcard-1] = fgetcsv($dataarray[$viewcard-1],4000, ":")) { but I think that I would run into the same problems. What's the best way to go about not allowing user input mess up this script? I've pasted the whole (faulty) script below: <?php // Text file for saving cards $cardfile="cardfile.txt"; // Load Cardflie into array and view specified file. $dataarray=file("$cardfile"); $httpcounter=0; $tok = strtok($dataarray[$viewcard-1],":"); /* Commented out stuff I was trying. while ($dataarray[$viewcard-1] = fgetcsv($dataarray[$viewcard-1],4000, ":")) { string strtok(string arg1, string arg2); $tok = strtok($string," "); while($tok) { echo "Word=$tok<br>"; $tok = strtok(" "); array fgetcsv(int fp, int length, string [delimiter]); while ($data = fgetcsv($fp,1000, ",")) { $num = count($data); print "<p> $num fields in line $row: <br>"; $row++; for ( $c=0; $c<$num; $c++ ) print $data[$c] . "<br>"; */ while($tok){ if ($httpcounter == 0) { echo ""; } elseif($httpcounter == 1){ echo "<table BORDER=0 WIDTH=\"100%\">\n<tr>\n<td ALIGN=CENTER VALIGN=TOP>\n<IMG src=\"graphics/$tok\">\n</td>\n "; } elseif ($httpcounter == 2) { echo "<td VALIGN=TOP>\n<table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=2 WIDTH=\"100%\">\n<tr>\n<td>\n<b>To:</b>\n</td>\n<td>\n<a href=\"mailto:$tok\">"; } elseif ($httpcounter == 3) { echo "$tok</a>\n</td>\n</tr>\n"; } elseif ($httpcounter == 4) { echo "<tr>\n<td>\n<b>From:</b></td>\n<td>\n<a href=\"mailto:$tok\">"; } elseif ($httpcounter == 5) { echo "$tok</a>\n</td>\n</tr>\n</table>\n"; } elseif ($httpcounter == 6) { echo "<B>Message:</B><BR>$tok</td>\n</td>\n</tr>\n</table>\n"; } else { } $tok = strtok(":"); $httpcounter++; } echo "Would you like to send a <A HREF=\"send.phtml\">reply card</A>?\n"; ?> Thanks! Mike -- Mike A. Gifford OpenConcept Consulting Mike@OpenConcept.On.Ca Http://www.OpenConcept.On.Ca Web Design, Hosting, Graphic Design & Site Promotion Featured Site --> Http://WTOAction.Org

« previous php.general (#4808) next »