I have a long string, like this:

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

This is a part of a SQL Insert query, and comma separated values are columns.
How can I explode this string by columns, if I have in the third columns a string, which also contains commas?

link|improve this question
feedback

2 Answers

You can use the str_getcsv function to parse a comma-separated list of values:

$parsed_array = str_getcsv($string, ',', "'"); //Return value: array
// Separated by :  ,  (comma)
// Enclosused by:  '  (single quote)
link|improve this answer
Thanks! It works perfectly! – user1116489 Dec 26 '11 at 15:57
feedback

Try 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);
link|improve this answer
Thanks! It works too :) – user1116489 Dec 26 '11 at 15:58
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.