fgetcsv

How to specify encoding while process csv file in PHP?

流过昼夜 提交于 2019-12-01 22:45:54
<?php $row = 1; $handle = fopen ("test.csv","r"); while ($data = fgetcsv ($handle, 1000, ",")) { $num = count ($data); print "<p> $num fields in line $row: <br>\n"; $row++; for ($c=0; $c < $num; $c++) { print $data[$c] . "<br>\n"; } } fclose ($handle); ?> The above comes from php manual,but I didn't see where to specify the encoding(like utf8 or so) Try to change the locale. Like it says below the example in the manual you gave: Note: Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function. Suggested

converting fgetcsv response into specific json

馋奶兔 提交于 2019-12-01 15:31:15
I have a .CSV file with headings: Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, Business Address, IsInternational. First row: Contact1, Contact1, True, True, Business, 123 Fake St, False Remaining rows not important, its just more like that - examples. I have a few rows of data in there. I need to get it into this format json: { Description:'Desc_47AE3208-87F5-4BBA-BE40-AA4130AB4768', SurnameBusinessName:'Name_Business', IsCustomer:true, IsSupplier:true, Addresses: [ {AddressType:'Business',Line1:'addr1_bus',IsInternational:false}, {AddressType:'Postal',Line1:'addr1_pos'

Display last entered data from CSV file in PHP? [closed]

为君一笑 提交于 2019-12-01 14:42:08
I have a CSV file with an initial header row and an unknown number of rows. The row format is: name_data, email_data, cell_data, dob_data I'd like to open the CSV file and then depict the data from the last entered row in a table, like this: Name: name_data Email: email_data Cell: cell_data D.O.B.: dob_data I'm thinking I can use fgetcsv() but I'm not sure how to parse the data once I get it. Any ideas? Thanks - Joe Seems inefficient to parse every line of the file with fgetcsv() when you only care about the first and last line. As such, I'd use file() and str_getcsv() : $rows = file('data.csv

converting fgetcsv response into specific json

天涯浪子 提交于 2019-12-01 14:24:44
问题 I have a .CSV file with headings: Description, BusinessSurname, IsCustomer, IsSupplier, AddressType, Business Address, IsInternational. First row: Contact1, Contact1, True, True, Business, 123 Fake St, False Remaining rows not important, its just more like that - examples. I have a few rows of data in there. I need to get it into this format json: { Description:'Desc_47AE3208-87F5-4BBA-BE40-AA4130AB4768', SurnameBusinessName:'Name_Business', IsCustomer:true, IsSupplier:true, Addresses: [

Display last entered data from CSV file in PHP? [closed]

£可爱£侵袭症+ 提交于 2019-12-01 12:34:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have a CSV file with an initial header row and an unknown number of rows. The row format is: name_data, email_data, cell_data, dob_data I'd like to open the CSV file and then depict the data from the last entered row in a table, like this: Name: name_data Email: email_data Cell: cell_data D.O.B.: dob_data I'm

Import from CSV in PHP (mysql) shows characterses like '???'

◇◆丶佛笑我妖孽 提交于 2019-12-01 09:45:34
问题 When I am importing from CSV, it is showing me characters like ??? My DB and tables are aleady set to utf8 unicode ci and arabic and other data shows properly when i do select * But when I import from CSV and use below code. It returns me uploaded successfully but data is shown as ??? in employee name Please find code below Import From CSV... <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php header('Content-Type: text/html; charset=utf-8'); mb_internal_encoding('UTF

Find if a value exist in a CSV file with PHP

我的未来我决定 提交于 2019-12-01 08:20:25
This is my code to check if a row of my .csv file contains a specific name, but it does not work. I think it has something to do with the if statement. $file_handle = fopen("sources.csv", "r"); while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); if ($line_of_text[0] = 'paul') { echo 'Found'; } } fclose($file_handle); I am trying to check in sources.csv files, for the name 'Paul' . I can't use a database like MySQL for technical reasons. Since you haven't provided a sample of your file, am submitting the following as an alternative. Do note that in your present code, you

How to make the header row be skipped in my while loop using fgetcsv?

浪尽此生 提交于 2019-12-01 05:15:10
问题 I can't get the new code I've written to skip the first row (header) as the code I was using before did (see bottom). I'm not receiving any errors, but just can't get it to omit first row. $file = fopen($uploadcsv,"r"); $column_headers = array(); $row_count = 0; while(!feof($file)) { if ($row_count==0){ $column_headers = $file; } else { print_r(fgetcsv($file)); } ++$row_count; } fclose($file); Below is the old source that skipped the header, for reference and comparison. $handle = fopen(

fgetcsv skip blank lines in file

坚强是说给别人听的谎言 提交于 2019-12-01 03:41:34
I have this script that I did, it basically grabs all the files in my "logs" folder and merge them all in one array file, my only problem is that, sometimes the script breaks if there is blank line or empty line! how can I tell it to automatically skip blank empty lines and go to next? blank lines are not necessarily at the top or bottom! could be in the middle of the csv file <?php $csv = array(); $files = glob('../logs/*.*'); $out = fopen("newfile.txt", "w"); foreach($files as $file){ $in = fopen($file, "r"); while (($result = fgetcsv($in)) !== false) { $csv[] = $result; } fclose($in);

PHP Using fgetcsv on a huge csv file

别等时光非礼了梦想. 提交于 2019-12-01 01:33:55
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, ideally with PHP so I can schedule a CRON to run the script at my web hosting provider every day. I have