Solved

function to clean csv data

Posted on 2009-03-31
10
678 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

758 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

19 Experts available now in Live!

Get 1:1 Help Now