douyan1244 2013-12-29 11:29
浏览 159
已采纳

当列值本身包含逗号时,以逗号分隔的CSV文件进行爆炸

I am reading a comma deliminated CSV file line by line and then separate each column value using PHP explode function. The problem is that there are some columns which itself have comma (,) values in it so they are also exploded.

A row of data:

03,1392,06,1000,1,"1000,36,21,68",4,AF,TJ,AF,44071000

Here "1000,36,21,68" must be considered as a single value but PHP explode also breaks it. I know this is how explode works but is there any alternate function which can be used in this case. Also i would need to remove the double quotes (") from both sides from this value.

  • 写回答

1条回答 默认 最新

  • dongxinpa3101 2013-12-29 11:31
    关注

    Don't try using explode and parsing it yourself:

    use PHP's built-in str_getcsv() function

    or use fgetcsv() to read and parse each line directly from file

    EDIT

    If you're feeling really adventurous, you can use SPL to read and parse the file

    $file = new SplFileObject("data.csv");
    while (!$file->eof()) {
        var_dump($file->fgetcsv());
    }
    

    or

    $file = new SplFileObject("data.csv");
    $file->setFlags(SplFileObject::READ_CSV);
    foreach ($file as $fields) {
        var_dump($fields);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算