The Quick & Dirty:

  • 9+ years in the biz, 10 messing around
  • All the latest in HTML, CSS, JS & PHP (Sorry, no .net or Ruby skills yet)
  • Cross-browser compatible code from Photoshop or Illustrator files.. or napkins!
  • Custom Specialities in Wordpress theming & plugins, Twitter & jQuery
  • Subversion, server-log analysis, and blocking hack-attempts (of late).
  • Data/Project Geek. I ♥ timelines.

I’ve compiled some handy PHP functions I’ve had to whip up. More extensive code-samples are also available.

Wordpress-from-Photoshop/Illustrator

Wordpress-from-static HTML

Wordpress-from-Photoshop

Randomly writing CSV data in PHP

Today’s project: add or replace values in a comma-separated-value file. The application is for tag-counting by week, so the first column is effectively an ID column & the rest (for me) are integers. But here’s the code with some sample data grabbed from a random Google search..

[sourcecode lang="php"]
/************** Vars ***************/

$str="id,age,sex,region,income,married,children,car,save_act,current_act,mortgage,pep
ID12101,48,FEMALE,INNER_CITY,17546.0,NO,1,NO,NO,NO,NO,YES
ID12102,40,MALE,TOWN,30085.1,YES,3,YES,NO,YES,YES,NO
ID12103,51,FEMALE,INNER_CITY,16575.4,YES,0,YES,YES,YES,NO,NO
ID12104,23,FEMALE,TOWN,20375.4,YES,3,NO,NO,YES,NO,NO
ID12105,57,FEMALE,RURAL,50576.3,YES,0,NO,YES,NO,NO,NO
ID12106,57,FEMALE,TOWN,37869.6,YES,2,NO,YES,YES,NO,YES
ID12107,22,MALE,RURAL,8877.07,NO,0,NO,NO,YES,NO,YES
ID12108,58,MALE,TOWN,24946.6,YES,0,YES,YES,YES,NO,NO
ID12109,37,FEMALE,SUBURBAN,25304.3,YES,2,YES,NO,NO,NO,NO
ID12110,54,MALE,TOWN,24212.1,YES,2,YES,YES,YES,NO,NO
ID12111,66,FEMALE,TOWN,59803.9,YES,0,NO,YES,YES,NO,NO
ID12112,52,FEMALE,INNER_CITY,26658.8,NO,0,YES,YES,YES,YES,NO
ID12113,44,FEMALE,TOWN,15735.8,YES,1,NO,YES,YES,YES,YES
ID12114,66,FEMALE,TOWN,55204.7,YES,1,YES,YES,YES,YES,YES
ID12115,36,MALE,RURAL,19474.6,YES,0,NO,YES,YES,YES,NO";

$tags2find_arr=array();
$tags2find_arr['ID12106']=array(1=>1,4=>100); //additive numbers, or replacing?
$tags2find_arr['ID12102']=array(1=>1,4=>200);
$tags2find_arr['ID12114']=array(1=>1,4=>300);

$fn=’test.txt’;
/************** Runtime ***************/

writeCSV($fn,$str);
writeRandCSV($fn,$tags2find_arr,’add’);

/************** Functions ***************/

function writeCSV($fn=”,$str=”){

$fh=fopen($fn,’w’);
fwrite($fh,$str);
fclose($fh);
}
/****************************/
function writeRandCSV($fn,$tags2find_arr,$addreplace=’add’){
$fl=filesize($fn);
$fh=fopen($fn,’r+’); //open for reading, and writing, but don’t kill the contents
$str=fread($fh, $fl);

foreach($tags2find_arr as $tag2find=>$csv_data_addreplace){ //$tag2find=’ID12108′;
//find the line in the str //alt: explode by ‘\n’ or ‘\r’ & get an array & it’s size.. find in array, truncate array,implode & find strlen’s for loc & write-len
$readtagloc=strpos($str,$tag2find.’,’); //this could be a problem for smaller tags found in larger.. throw a \n before & a , after?
//echo “\r
“.$tag2find.” @”.$readtagloc;

//find it’s corollary in the file
fseek($fh,$readtagloc);
$fcsv_arr=fgetcsv($fh); //should only get one line!
//for($i=0;$i //$fcsv_arr[$i]=$fcsv_arr[$i]+$csv_data_addreplace[$i]; //58->59
foreach($csv_data_addreplace as $csv_idx=>$csv_addreplace){
if($addreplace==’add’) $fcsv_arr[$csv_idx]+=$csv_addreplace; //58->59
else $fcsv_arr[$csv_idx]=$csv_addreplace;
}
//print_r($fcsv_arr);

/******************************/
//echo ‘\r
‘.ftell($fh).’ or ‘.(ftell($fh)-strlen(implode(‘,’,$csv_arr))-1 );
fseek($fh,$readtagloc); //back to beginning of line to write now
//$read=fread($fh, $readlen);
//hoping the pointer is still in the same loc & not @ line-end!

$ok[$tag2find]=fputcsv($fh,$fcsv_arr,’,’);
$sz=(strlen(implode(‘,’,$fcsv_arr))+1);

//if($ok===false){ echo “FAILURE”; }
//if($ok==$sz){ echo “\r
written length ok:”.$ok; }
//else { “\r
written length not ok:”.$ok.’<'.$sz; }

} //end foreach tag
$ok[]=fclose($fh);
return $ok;

} //close function
/******************************/
?>
[/sourcecode]

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
Posted in codeRelease, PHP Tagged , , , , , , , , , Comments Off

Comments are closed.