-1

I want to remove the ',' dellimiter between " " only . Exmp : "Alibris: Books, Music, & Movies"

Output of 1st row should be

256,2653,"Alibris: Books  Music  & Movies",50263394,05/14/2013,07:44,50263394,114.85,1,5.74,05/14/2013,08:10

example of array here

Array
(
    [0] => 256,2653,"Alibris: Books, Music, & Movies",50263394,05/14/2013,07:44,50263394,114.85,1,5.74,05/14/2013,08:10,
    [1] => 256,2653,"Alibris: Books, Music, & Movies",50327805,05/21/2013,23:03,50327805,-6.99,1,-0.35,05/22/2013,07:10,
    [2] => 256,2653,"Alibris: Books, Music, & Movies",50327805,05/21/2013,23:03,50327805,6.99,1,0.35,05/22/2013,00:10,
    [3] => 527,36777,BuySKU,920130525042340263061,05/24/2013,20:25,1390043,"1,170.73",11,58.54,05/24/2013,20:55,
)
Manoj Rathore
  • 127
  • 1
  • 14

3 Answers3

2

Here's the approach I suggest you take:

The key is that you need to get the string broken up into its component parts, rather than trying to modify certain commas but not others.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
1

You can do it this way

,(?!([^"]*"[^"]*"[^"]*)+$|[^"]*$)

replace it with empty string

This would match , only if there are odd number of " ahead.So it would match , if its between "

DEMO

So,it would be

$pattern = "/,(?!([^\"]*\"[^\"]*\"[^\"]*)+$|[^\"]*$)/"; 
$replace = ""; 
$line = preg_replace($pattern,$replace,$line); 

If you want to split it with , only its outside " use

 ,(?=([^"]*"[^"]*"[^"]*)+$|[^"]*$)

So it would be

preg_split('/,(?=([^\"]*\"[^\"]*\"[^\"]*)+$|[^\"]*$)/', $text);
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

You can use str_getcsv and a str_replace in a closure to accomplish this

$rec = str_getcsv($line);
$rec = array_map(
     function($x){
          return str_replace(",", "", $x); 
     },$rec);

Now you end up with an array that you can use in a call to fputcsv

If you absolutely need the value as a string you can use a the str_putcsv implementation inplemented here.

Note that this syntax (inline closures as well as the str_getcsv function) will only work in php 5.3+

Community
  • 1
  • 1
Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • Not working I m using this code :: foreach ($wrong_row as $khy => $vae) { $vae = str_replace(",","",$vae); } – Manoj Rathore Jul 02 '13 at 04:57
  • @intekhabkhan That is because within the foreach $vae is passed by value instead of by reference so editing the value does nothing to the original array. – Orangepill Jul 02 '13 at 05:02