Reviews, updates and in depth guides to your favourite mobile games - AppGamer.com
|
|
Couple of php q's |
Page: 1 | Reply |
Dec 17th 2001 | #23669 Report |
Member since: Nov 25th 2001 Posts: 2 |
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?? [PHP] $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++; } [/PHP] I know that this will work ok for small files, but what would be a cut-off for 'small file' ?? |
Reply with Quote Reply |
Dec 18th 2001 | #23727 Report |
Member since: Jun 30th 2001 Posts: 447 |
1) Read the file into a scalar variable ($something) and run the stripslashes() function on it. 2) I don't believe there is any way to do this in PHP, at least to my knowledge. Your example seems pretty efficient, and that's how I'd do it. Keep in mind, that PHP wasn't really designed for text-handling. If you're worried about speed, try Perl or C. |
Reply with Quote Reply |
Dec 18th 2001 | #23735 Report |
Member since: Nov 25th 2001 Posts: 2 |
Thanks for the reply. Here was my solution (with some pointers from vBulletin forums) [PHP]include ( "./include/db_connect.php" ) ; $fd = fopen ("data.csv", "r"); // loop until EOF while (!feof ($fd)) { // read one line, up to 4KB $buffer = fgets($fd, 4096); // if line is empty then quit if ($buffer == '') exit(); else { // strip all quotes from line $buffer = str_replace("\"", '', $buffer) ; // strip all comma's from line $buffer = str_replace(",", '', $buffer) ; // explode to get each field from line $data = explode(";", $buffer); // do some stuff to enter fields into DB } } fclose ($fd);[/PHP] In case anyone finds it useful. |
Reply with Quote Reply |
Dec 18th 2001 | #23806 Report |
Member since: Mar 20th 2001 Posts: 3367 |
wonder how i'm gonna use that..lol waiting to go back to school on 2 Jan 2002 ... b4 i start programming again... php here i come again |
Reply with Quote Reply |
Page: 1 | Back to top |
Please login or register above to post in this forum |
© Web Media Network Limited. All rights reserved. No part of this website may be reproduced without written permission. Photoshop is a registered trademark of Adobe Inc.. TeamPhotoshop.com is not associated in any way with Adobe, nor is an offical Photoshop website. |