开发者

Split string by delimiter ignoring delimiter inside quotes

开发者 https://www.devze.com 2023-01-17 02:27 出处:网络
string开发者_C百科 from the file i\'m importing: 1,2,3,\"a\",\"b\",\"c\",\"1,2,3,4,5\",6,7 desired output would be an array split by the comma but ignoring any comma that falls between double quotes.

string开发者_C百科 from the file i'm importing: 1,2,3,"a","b","c","1,2,3,4,5",6,7

desired output would be an array split by the comma but ignoring any comma that falls between double quotes.

[0] = 1;
[1] = 2;
[2] = 3;
[4] = "a";
[5] = "b";
[6] = "c";
[7] = "1,2,3,4,5";
[8] = 6;
[9] = 7;

Needs to work with PHP 5.2 and below. I know the fget_csv function allows for this in PHP 5.3 but its not an option unfortunately.

Searched fairly extensively and so far no solutions for this that I can find. Any ideas?


You can do csv in PHP < 5.3:

$lines = array();
if (($handle = fopen('file.csv', 'r')) !== FALSE) {
    while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) {
  $lines[] = $line;
 }
    fclose($handle);
}
print_r($lines);

/* output:
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => a
            [4] => b
            [5] => c
            [6] => 1,2,3,4,5
            [7] => 6
            [8] => 7
        )

)
*/
0

精彩评论

暂无评论...
验证码 换一张
取 消