我正在尝试使用preg_split()
,但结果并不是我期望从函数中获得的结果。
我是php和整个preg_split()
场景的新手,对我来说似乎很难理解,至少在目前。
$row = "EL10,40,2019-02-06,55555,2019-01-06,[email protected],"Text , random text , 52555885/ 48484848484",Yes,One Two,Broke,2019-01-01,000.00,0.00,0.0,0.0,0.0,0.00,0.00,0.0,VRA "Morning";
$row_expl = preg_split('/(?:[^"]*"|)\K\s*(,\s*|$)/',$row);
我希望删除逗号分隔符,同时将逗号留在引号中。
一切似乎都有效,唯一的问题发生在最后。它为:VRA "Morning"
添加了额外的引号。结果如下:"VRA ""Morning"""
正则表达式实际上是您的问题的错误工具。定义分隔符和机箱字符的CSV解析器是您需要的工具。
str_getcsv('EL10,40,2019-02-06,55555,2019-01-06,[email protected],"Text , random text , 52555885/ 48484848484",Yes,One Two,Broke,2019-01-01,000.00,0.00,0.0,0.0,0.0,0.00,0.00,0.0,VRA "Morning')
str_getcsv
的默认分隔符是,
,封闭字符是"
,所以应该使用默认选项进行设置。你可以在这里看到更多关于这个功能的http://php.net/manual/en/function.str-getcsv.php。