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?

 

 

Link to comment
Share on other sites

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]);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.