fgetcsv

PHP Using fgetcsv on a huge csv file

放肆的年华 提交于 2019-11-30 21:06:27
问题 Using fgetcsv , can I somehow do a destructive read where rows I've read and processed would be discarded so if I don't make it through the whole file in the first pass, I can come back and pick up where I left off before the script timed out ? Additional Details: I'm getting a daily product feed from a vendor that comes across as a 200mb .gz file. When I unpack the file, it turns into a 1.5gb .csv with nearly 500,000 rows and 20 - 25 fields. I need to read this information into a MySQL db,

Converting CSV to array

浪子不回头ぞ 提交于 2019-11-29 02:12:42
I have this array with airport codes and city names (around 3500 lines). code,city "Abilene, TX ",ABI "Adak Island, AK ",ADK "Akiachak, AK ",KKI "Akiak, AK ",AKI "Akron/Canton, OH ",CAK "Akuton, AK ",KQA "Alakanuk, AK ",AUK "Alamogordo, NM ",ALM I need to convert that file into a php array. This is my code so far: if(($handle = fopen('test.csv', 'r')) !== FALSE) { while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE) { echo '<pre>'; print_r($data); echo '</pre>'; } fclose($handle); } Although I'm setting the delimiter and enclousure characters for the fgetcsv function, im getting this

fgetcsv() ignores special characters when they are at the beginning of line!

时光毁灭记忆、已成空白 提交于 2019-11-28 11:04:38
I have a simple script that accepts a CSV file and reads every row into an array. I then cycle through each column of the first row (in my case it holds the questions of a survey) and I print them out. The survey is in french and whenever the first character of a question is a special character (é,ê,ç, etc) fgetcsv simply omits it. Special characters in the middle of the value are not affected only when they are the first character. I tried to debug this but I am baffled. I did a var_dump with the content of the file and the characters are definitely there: var_dump(utf8_encode(file_get

Remove BOM () from imported .csv file

白昼怎懂夜的黑 提交于 2019-11-28 10:22:54
I want to delete the BOM from my imported file, but it just doesn't seem to work. I tried to preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $file); and a str_replace. I hope anybody sees what I'm doing wrong. $filepath = get_bloginfo('template_directory')."/testing.csv"; setlocale(LC_ALL, 'nl_NL'); ini_set('auto_detect_line_endings',TRUE); $file = fopen($filepath, "r") or die("Error opening file"); $i = 0; while(($line = fgetcsv($file, 1000, ";")) !== FALSE) { if($i == 0) { $c = 0; foreach($line as $col) { $cols[$c] = utf8_encode($col); $c++; } } else if($i > 0) { $c = 0; foreach($line as $col) {

CSVs without quotes not working with fgetcsv

巧了我就是萌 提交于 2019-11-28 08:31:11
I'm trying to parse CSV files uploaded by the user through PHP, but it's not working properly. I've uploaded several properly formatted CSVs and it worked fine, however; I have many users trying to import CSV files exported from Excel and they are having problems. I've compared the files to mine and noticed that the Excel files all lack quotes around the entries. Aside from that, they are identical. If I open it and save it with Open Office, without making any changes at all it works. So I'm fairly certain it's related to the quotes. My question is; how do I read these improperly formatted

how do i parse a csv file to grab the column names first then the rows that relate to it?

家住魔仙堡 提交于 2019-11-28 04:45:12
here is my csv column1,column2,column3,column4,column5 column1_row1,column2_row1,column3_row1,column4_row1,column5_row1 column1_row2,column2_row2,column3_row2,column4_row2,column5_row2 column1_row3,column2_row3,column3_row3,column4_row3,column5_row3 column1_row4,column2_row4,column3_row4,column4_row4,column5_row4 column1_row5,column2_row5,column3_row5,column4_row5,column5_row5 column1_row6,column2_row6,column3_row6,column4_row6,column5_row6 column1_row7,column2_row7,column3_row7,column4_row7,column5_row7 column1_row8,column2_row8,column3_row8,column4_row8,column5_row8 column1_row9,column2_row9

Remove Line From CSV File

蹲街弑〆低调 提交于 2019-11-27 16:14:53
I have .csv file with 4 columns. What's the easiest way to remove a line identical with the id of the first column? Here's where I got stuck: if($_GET['id']) { $id = $_GET['id']; $file_handle = fopen("testimonials.csv", "rw"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); if ($id == $line_of_text[0]) { // remove row } } fclose($file_handle); } Unfortunately, databases were not a choice. $id = $_GET['id']; if($id) { $file_handle = fopen("testimonials.csv", "w+"); $myCsv = array(); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); if ($id !

UTF-8 problems while reading CSV file with fgetcsv

僤鯓⒐⒋嵵緔 提交于 2019-11-27 12:39:12
I try to read a CSV and echo the content. But the content displays the characters wrong. Mäx Müstermänn -> Mäx Müstermänn Encoding of the CSV file is UTF-8 without BOM (checked with Notepad++). This is the content of the CSV file: "Mäx";"Müstermänn" My PHP script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php $handle = fopen ("specialchars.csv","r"); echo '<table border="1"><tr

Remove BOM () from imported .csv file

时光总嘲笑我的痴心妄想 提交于 2019-11-27 03:30:23
问题 I want to delete the BOM from my imported file, but it just doesn't seem to work. I tried to preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $file); and a str_replace. I hope anybody sees what I'm doing wrong. $filepath = get_bloginfo('template_directory')."/testing.csv"; setlocale(LC_ALL, 'nl_NL'); ini_set('auto_detect_line_endings',TRUE); $file = fopen($filepath, "r") or die("Error opening file"); $i = 0; while(($line = fgetcsv($file, 1000, ";")) !== FALSE) { if($i == 0) { $c = 0; foreach($line

UTF-8 problems while reading CSV file with fgetcsv

 ̄綄美尐妖づ 提交于 2019-11-26 16:01:21
问题 I try to read a CSV and echo the content. But the content displays the characters wrong. Mäx Müstermänn -> Mäx Müstermänn Encoding of the CSV file is UTF-8 without BOM (checked with Notepad++). This is the content of the CSV file: "Mäx";"Müstermänn" My PHP script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;