PHP:爆炸复杂的字符串

时间:2011-12-26 15:49:56

标签: php string explode

我有一个很长的字符串,如下所示:

11070, 'EP_LQ-630', 'LQ-630, 24 pin, A4, 360 cps, USB 1.1&LPT', NULL, 6438, 1, 45, 1, 5184, 0, 20, NULL, NULL, 432, 1, 5088, 0, 424, 1, 1, 4, 1, NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, 370, 1, 369, 1, 368, 1, 367, 1, NULL, NULL, NULL, NULL, NULL, 1, '8443', NULL, NULL, NULL, '07.12.2011 18:16:28', NULL, NULL, '8471604', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 366, 1, ';6438;6432;4873;', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL

这是SQL Insert查询的一部分,逗号分隔值是列 如果我在第三列中包含一个包含逗号的字符串,我该怎样才能按列分解这个字符串?

2 个答案:

答案 0 :(得分:8)

您可以使用str_getcsv函数来解析以逗号分隔的值列表:

$parsed_array = str_getcsv($string, ',', "'"); //Return value: array
// Separated by :  ,  (comma)
// Enclosused by:  '  (single quote)

答案 1 :(得分:5)

尝试str_getcsv

<?php
$s = "11070, 'EP_LQ-630', 'LQ-630, 24 pin, A4, 360 cps, USB 1.1&LPT', NULL, 6438, 1, 45, 1, 5184, 0, 20, NULL, NULL, 432, 1, 5088, 0, 424, 1, 1, 4, 1, NULL, NULL, NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, 370, 1, 369, 1, 368, 1, 367, 1, NULL, NULL, NULL, NULL, NULL, 1, '8443', NULL, NULL, NULL, '07.12.2011 18:16:28', NULL, NULL, '8471604', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 366, 1, ';6438;6432;4873;', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL";
$a = str_getcsv($s, ",", "'");
var_export($a);