Regular expression for parsing CSV in PHP

前端 未结 6 2215
离开以前
离开以前 2020-12-10 06:58

I already managed to split the CSV file using this regex: \"/,(?=(?:[^\\\"]\\\"[^\\\"]\\\")(?![^\\\"]\\\"))/\"

But I ended up with an array of stri

6条回答
  •  感动是毒
    2020-12-10 07:13

    preg_split('/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $line,-1,PREG_SPLIT_DELIM_CAPTURE);
    

    Has Problems with " inside of strings like "Toys"R"Us"

    So u should use instead:

    preg_split('/'.$seperator.'(?=(?:[^\"])*(?![^\"]))/', $line,-1, PREG_SPLIT_DELIM_CAPTURE);
    

提交回复
热议问题