如何解析包含逗号的值的CSV? [英] How to parse CSV with values that contain a comma?

查看:105
本文介绍了如何解析包含逗号的值的CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下字符串:

$str = 'one value, two value, "three, cool value", four value';

如何使它成为如下数组:

How would you make it an array as follows:

$arr = array('one value', 'two value', 'three, cool value', 'four value');

(这是关于CSV和值的所有包含逗号,因此双引号。)

(That's all about CSV and values that contain a comma and thus are double quoted.)

推荐答案

如果你真的从字符串解析它,使用 str_getcsv

If you are really parsing it from a string, use str_getcsv.

如果您正在从文件读取数据,请使用 fgetcsv

If you are reading data from a file, use fgetcsv.

这篇关于如何解析包含逗号的值的CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆