CodeFlash Posted October 13, 2008 Share Posted October 13, 2008 Hi, I've been working on a project that takes a TCP/IP data stream and sticks it into a txt file then I send it to the web to be imported into MySQL for a live data stream type Flash application but I've ran into a hicup. The program sending the data doesnt send it in lines, so its just one long stream and I want to break it up into multiple lines. Only problem is I can't find an easy way to do it, so I thought I would ask. I've googled and googled to no avail. So far this is what I have: $fcontents = file ('./Results.txt'); # expects the csv file to be in the same dir as this script for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode(";", $line); #if your data is comma separated # instead of tab separated, # change the '\t' above to ',' $sql = "INSERT INTO Result values (\"".implode("\",\"", $arr)."\")"; mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br>\n"; } } Right now its a a semicolon delimited text file with a | at then end of each line. So is there anyway to easily add a line delimiter? Thanks so much! Cody Link to comment https://forums.phpfreaks.com/topic/128166-solved-php-import-csv-to-mysql-line-delimiter/ Share on other sites More sharing options...
ratcateme Posted October 13, 2008 Share Posted October 13, 2008 on the script that takes the stream and writes it to the file you could do this $result = str_replace("|","\r\n",$result); that will put the lines in your file or on the script you posted you could say $fcontents = file_get_contents('./Results.txt'); $fcontents = explode("|",$fcontents); Scott. Link to comment https://forums.phpfreaks.com/topic/128166-solved-php-import-csv-to-mysql-line-delimiter/#findComment-663799 Share on other sites More sharing options...
CodeFlash Posted October 13, 2008 Author Share Posted October 13, 2008 Scott, thank you so much! Worked like a charm, sorry I had to ask such a dumb question but I was stumped...lol Thanks again! Cody Link to comment https://forums.phpfreaks.com/topic/128166-solved-php-import-csv-to-mysql-line-delimiter/#findComment-663800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.