有没有办法用 preg_split 替换一些引号并省略一些引号?
Is there a way to replace some quotation marks with preg_split and to leave some out?
我正在尝试使用 preg_split()
,但结果不是我期望从函数中获得的结果。
我是 php 和整个 preg_split()
场景的新手,我似乎很难理解,至少目前是这样。
$row = "EL10,40,2019-02-06,55555,2019-01-06,ar@email.com,"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,ar@email.com,"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.
查看有关该功能的更多信息
我正在尝试使用 preg_split()
,但结果不是我期望从函数中获得的结果。
我是 php 和整个 preg_split()
场景的新手,我似乎很难理解,至少目前是这样。
$row = "EL10,40,2019-02-06,55555,2019-01-06,ar@email.com,"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,ar@email.com,"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.