在PHP中解析CSV的正则表达式 [英] Regular expression for parsing CSV in PHP

查看:160
本文介绍了在PHP中解析CSV的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法使用这个正则表达式拆分CSV文件:
/,(?=(?:[^ \] \[^ \] \)(?![^ \] \))/

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

但是我结束了一个数组包含开头和结尾双引号的字符串。
现在我需要一个正则表达式,将剥离这些字符串的分隔符双引号。

But I ended up with an array of strings that contain the opening and ending double quotes. Now I need a regex that would strip those strings of the delimiter double quotes.

据我所知,CSV格式可以封装字符串在双引号,并且已经是字符串一部分的所有双引号都加倍。例如:

As far as I know the CSV format can encapsulate strings in double quotes, and all the double quotes that are already a part of the string are doubled. For example:

我的其他猫

成为

Myothercat

我基本需要的是一个正则表达式, 2 - 舍入)双引号。

What I basically need is a regex that will replace all sequences of N doublequotes with a sequence of (N/2 - rounded down) double quotes.

还是有更好的方法吗?
提前感谢。

Or is there a better way ? Thanks in advance.

推荐答案

有读取csv文件的功能: fgetcsv

There is function for reading csv files: fgetcsv

这篇关于在PHP中解析CSV的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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