PHP fgetcsv数组以逗号分隔问题

时间:2013-07-01 19:16:39

标签: php mysql fgetcsv

我的fgetcsv命令似乎有问题而不是逗号分隔,基本上发生的事情是我的数据只是数组中一个非常长的字符串。我需要数据为数组格式,以便implode函数能够正常工作以上传到mysql数据库。

目前mysql上传看起来像......

INSERT INTO metars VALUES('PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0"

这应该在每个元素周围都有单引号,但是因为它是一个字符串,所以内爆不起作用= \

<?php

require_once("../config/dbmetar.php");

$file = "metars.csv";
$db = new mysqli(DB_HOST_METAR, DB_USER_METAR, DB_PASS_METAR, DB_NAME_METAR);
$r = 0;

if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 3000, ",", '"')) !== FALSE) {
        if ($r >= 6) { //skips the header
            foreach($data as $i => $content) {

                $data[$i] = $db->real_escape_string($content);

            }

            //echo "INSERT INTO metars VALUES('" . implode("','", $data) . '"' ;
            //echo var_dump($data);
            $db->query("INSERT INTO metars VALUES('" . implode("','", $data) . "');");

        }
        $r++;   
    }
    fclose($handle);
}

?>

以下是csv文件的示例

No errors
Max results hit (1000): not all possible results returned; 
59 ms
data source=metars
948 results
raw_text,station_id,observation_time,latitude,longitude,temp_c,dewpoint_c,wind_dir_degrees,wind_speed_kt,wind_gust_kt,visibility_statute_mi,altim_in_hg,sea_level_pressure_mb,corrected,auto,auto_station,maintenance_indicator_on,no_signal,lightning_sensor_off,freezing_rain_sensor_off,present_weather_sensor_off,wx_string,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,sky_cover,cloud_base_ft_agl,flight_category,three_hr_pressure_tendency_mb,maxT_c,minT_c,maxT24hr_c,minT24hr_c,precip_in,pcp3hr_in,pcp6hr_in,pcp24hr_in,snow_in,vert_vis_ft,metar_type,elevation_m"
"PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0"
"CYOD 011131Z 18002KT 1/2SM FG FEW250 RMK CI0,CYOD,2013-07-01T11:31:00Z,54.4,-110.28,,,180,2,,0.5,,,,,,,,,,,FG,FEW,25000,,,,,,,LIFR,,,,,,,,,,,,SPECI,544.0"
"CYYD 011131Z AUTO VRB06KT 5SM -RA BR FEW007 BKN070 OVC084 15/14 A3005 RMK PRESRR PCPN 1.0MM PAST HR SLP173 DENSITY ALT 1900FT,CYYD,2013-07-01T11:31:00Z,54.82,-127.18,15.0,14.0,0,6,,5.0,30.050198,1017.3,,TRUE,,,,,,,-RA BR,FEW,700,BKN,7000,OVC,8400,,,MVFR,,,,,,,,,,,,SPECI,523.0"
"KNSE 011131Z 00000KT 10SM -RA FEW025 BKN070 BKN120 BKN250 23/19 A2980 RMK AO2 WSHFT 1045 RAB29 P0000 $ ,KNSE,2013-07-01T11:31:00Z,30.72,-87.02,23.0,19.0,0,0,,10.0,29.799213,,,,TRUE,TRUE,,,,,-RA,FEW,2500,BKN,7000,BKN,12000,BKN,25000,VFR,,,,,,0.0050,,,,,,SPECI,61.0"

我一直试图解决这个困境大约6个小时没有运气,我尝试了不同的csv拉方法,如str_getcsv,fgetcsv,我已经尝试了foreach和for ...简化版是什么我在下面,是更新标题信息的最简单方法。

目的:这个php脚本将在cron调度程序中每隔60秒-150秒运行一次,如果你对此有任何其他建议,我也会很感激。

我非常感谢这方面的任何帮助,

谢谢,

-Mikael

1 个答案:

答案 0 :(得分:0)

您可能需要考虑将file()用于此类操作,以便可以修剪前几行并修剪CSV字符串末尾的引号。您传递的fgetcsv参数是用引号括起的每个列,而不是整个字符串。

然后,您可以使用str_getcsv()

示例(未经测试):

<?php
$file = file("metars.csv");
for($i = 0; $i < 6; $i++) array_shift($file);
foreach($file as $line) {
  $line = ltrim($line, '"');
  $line = rtrim($line, '"');
  $csv  = str_getcsv($line);
  /** do query, preferably a prepared statement **/
}
?>

当然,如果您的文件很大 - 这可能不是最佳解决方案。