Saturday, February 17, 2007

PHP to the rescue

Greetings all,

We recently purchased a business listing from certified-lists to aid in our project-contact marketing effort. They supplied us with a niche list for a reasonable price.

The list is provided in CSV format, however our lead designer was still spending a lot of time creating the mailing labels. Our lead engineer threw together a quick PHP script to take the mailing fields and combine them into one cell. This improved the copy and paste time for our mailing label maker.

Here's a copy of the script:

/* Quick script to take particular CSV fields
and arrange them into a single cell.

When: Who: What:
----- ---- -----
13-Feb-07 BJS Created.
*/


function stripQuotes(&$str)
{
$tmpStr = explode("\"", $str);
if ( isset($tmpStr[1]) )
$str = $tmpStr[1];
else
$str = $tmpStr[0];
}


$fHandle = fopen("text_list.csv", "r"); /* open for reading */
$cHandle = fopen("new_list.csv", "w"); /* open and truncate for writing */
$newCSV = '';
//$count= 0;
while (! feof($fHandle) )
{
$lineStr = fgets($fHandle, 2048);
list( $executiveName,
$tmp,
$companyName,
$address,
$tmp,
$city,
$state,
$zipCode,
$zipFour) = explode(",", $lineStr);

stripQuotes($executiveName);
stripQuotes($companyName);
stripQuotes($address);
stripQuotes($city);
stripQuotes($state);
stripQuotes($zipCode);
stripQuotes($zipFour);

$newCSV .= "\"$executiveName $companyName $address "
. "$city $state $zipCode-$zipFour\"\n";
/*
if ( $count > 2)
exit;
$count++;
echo $newCSV;
*/
}

fwrite($cHandle, $newCSV);

fclose($cHandle);
fclose($fHandle);

?>

It takes the desired fields, (everything except $tmp on the list( ) = explode) and builds them into a new CSV file, this time all in the same cell.

That wraps it up for today..thank you PHP!

- psl

0 Comments:

Post a Comment

<< Home