Solved

function to clean csv data

Posted on 2009-03-31
10
683 Views
Last Modified: 2012-05-06
I have a csv file comma delimited. Some of the data is double quote encaps where address field has comma caused by user input from web form.

Ie. 12345,"235 main street, apt3",boston,ma,12431

When i try to use putcsv function and import into sql the extra comma that is encaps is causing apt3 to write into the city column and city into the state column, etc.

How can i clean this up before importing?

I tried this but dont work.
<?

 

$file_path = "/my/path";

$file = $file_path . "filename.csv";

 

$handle = fopen("$file", "r"); 

if ($handle)

{

$array = explode("\n", fread($handle, filesize("$file")));

}

 

$total_array = count($array);

$i = 0;

 

$handle2 = fopen('filename2.csv', 'w');

 

while($i < $total_array)

{

	

$data = explode(",", $array[$i]);

 

fputcsv($handle2, $data, $sSeparator='|', $sEnclosure='"');

 

$i++;

}

 

fclose($handle); 

 

?>

Open in new window

0
Comment
Question by:pdheady
  • 3
  • 3
  • 3
10 Comments
 
LVL 3

Accepted Solution

by:
garlix earned 500 total points
ID: 24032310
The code below will find all , within " and " and replace them with a ..

Hope that will be usefull!

$test = '12345,"235 main street, apt3",boston,ma,12431';

$csvOk = preg_replace_callback('/"(.*)"/', create_function('$matches',

            'return \'"\' . preg_replace(\'/,/\', \'.\', $matches[1]) . \'"\';'), $test);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24032378
@pdheady:

What if you decided to do without commas in the address?  The USPS does not use, or need, them.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24032471
... and for that matter, the Geocoders from Google and Yahoo do not need commas inside the address components, either.  Just a thought. ~Ray
0
 
LVL 1

Author Comment

by:pdheady
ID: 24032510
I have no control over someone entering in a comma in the address field at the moment. Another company handles the front end web form.

I will strip it out when they post to our system but for now need to clean up existing data. I need something that reads the csv and locates the double quote field and pulls the comma out, then exports to new csv file.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:pdheady
ID: 24032516
I was able to do a search and replace using excel feature but would prefer using php to do the work.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24032545
Understood - havent' tested it , but garlix answer can probably be useful.  Good luck with it, ~Ray
0
 
LVL 1

Author Comment

by:pdheady
ID: 24032621
Thanks, I guess based on that my question is how to implement garlix suggestion to strip from ARRAY $data
0
 
LVL 3

Assisted Solution

by:garlix
garlix earned 500 total points
ID: 24032710
If you got an array where each post is a row of csv data. Then the following ought to do the trick :-)

Good luck!

function alterRow(&$item, $key) {

    $item = preg_replace_callback('/"(.*)"/', create_function('$matches',

                'return \'"\' . preg_replace(\'/,/\', \'.\', $matches[1]) . \'"\';'), $item);

}
 

array_walk($rows, 'alterRow');

Open in new window

0
 
LVL 3

Expert Comment

by:garlix
ID: 24037060
Did you get it to work? :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WordPress syntax 2 25
phpmailer in WHILE loop - weird results 10 27
hiding/removing php extension - best practice 4 24
Scope of $_SESSION 17 25
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now