ollyno1uk Posted April 3, 2009 Share Posted April 3, 2009 Hi I wrote a script a while back that imports a pipe csv files into my database. It worked great until the people who make the file started using some pipes within the string. The strings are separated with quotations but I do not know how to modify this script to not explode the pipes that are contained within; Perhaps someone can point me in the right direction. $url = 'url of feed'; $content = file_get_contents($url); $array = explode("\n", $content); foreach($array as $line_number=>$line){ $column = explode('|', $line); $query = "insert into table set column1 = $column[0], column2 = $column[1], column3 = $column[2]; Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 PHP has some CSV handling routines especially for reading these files. http://php.net/fgetcsv Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/#findComment-800233 Share on other sites More sharing options...
ollyno1uk Posted April 3, 2009 Author Share Posted April 3, 2009 wow that was quick - thanks I have looked at this function before but as a bit of a php newbie, I note that most examples are using a while() rather than my foreach() hence I stayed way from it. I will experiment some more with it Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/#findComment-800241 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 You're using file_get_contents to get the contents of your CSV file so it wouldn't be very difficult to modify your script to read in using fgetcsv() Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/#findComment-800245 Share on other sites More sharing options...
ollyno1uk Posted April 3, 2009 Author Share Posted April 3, 2009 with the fgetcsv() will I need to actually get teh csv file to my server first before it works? Previously I was just getting the data from the url. It does not seem to like it changing to the fgetcsv() Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/#findComment-800272 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 I presume so - you can use file_get_contents to grab the content, create a file on the server something like: $filename='tmp'.time().'.csv'; Then read the file in using fgetcsv() once you open() the file you created for reading. Once finished, unless you need to keep the contents of the file you can use unlink() to delete it. unlink($filename); Link to comment https://forums.phpfreaks.com/topic/152374-importing-a-pipe-csv-files-into-a-db-issue/#findComment-800381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.