Jump to content

[SOLVED] Splitting CSV data which inlcudes comma-separated numerical values


sh44n

Recommended Posts

Sample Data

--------------

"Costs of Goods & Services / Total Expenses","-88,105","-91,960","-46,339","-47,400","-48,692","-52,892","-41,672","-43,334","-46,671","-43,203"

 

This is a CSV data including comma-separated numerics. The problem is when i try to split it with the following code, it also splits it on the basis of decimals,

 

$arr_tag_pieces = explode(',',$value);

 

I tried a trick by splitting it as follows i.e. splitting on the basis of [","]

 

$arr_tag_pieces = explode('","',$value);

 

but the problem is that the first element and last element of the array receives the double quotes in the beginning and the end respectively.

 

$arr_tag_pieces[0] = "Costs of Goods & Services / Total Expenses

$arr_tag_pieces[10] = -43,203"

 

 

any suggestions/way-outs?

 

 

All your data is stored in $r[1].

 

<?php
$s = '"Costs of Goods & Services / Total Expenses","-88,105","-91,960","-46,339","-47,400","-48,692","-52,892","-41,672","-43,334","-46,671","-43,203"';
$p = '_"([^"]+?)"_i';

preg_match_all($p, $s, $r);

var_dump($r[1]);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.