PHP获取逗号分隔的值,这些值不包含在内


PHP get comma separated values which are not enclosed

具有如下字符串:

5, "hi all, this is a comma", 'text without comma', 5,'a',',',","

我想把字符串分成一个数组,但仍然有一些值包含逗号(用单引号或双引号括起来)

最快的正则表达式是什么(在这里爆炸不起作用)

我可以使用str_getcsv,但字符串有时用单引号括起来,有时用双引号括起来!需要更智能的

您可以使用csv解析器,或者preg_split使用以下模式:

$pattern = '~(?:''[^'']*''|"[^"]*"|)'K(,|$)~';