jschofield Posted April 10, 2007 Share Posted April 10, 2007 Hello all. I am trying to remove spaces in a line in a text file using the trim function. I am pretty sure it works but I am using it in the wrong way and not sure how to figure it out. Please help. Below is my script. It has an include script but that script is just for logging. Thanks for everyone's help. Data: 0200,000970 ,LILY *Here it has a comma about 25 spaces after the name LILY. Those are the spaces I am looking to remove. Script: <?php $inputfile = "C:/SMDIM/Data/allstu.TXT"; $outputfile = "C:/SMDIM/Data/Wisconsin_Student_Data.csv"; $logfilename = "C:/SMDIM/Logs/Wisconsin_Student_Log.txt"; include'C:/SMDIM/Translators/importlibrary.inc.php'; $count = 0; //open the file if(!$outfilefp = fopen($outputfile, "w")) { wlogdie("Failed to open $outputfile"); }else{ wlog("Opened $outputfile for writing"); } if(!$inputfp = fopen($inputfile, "r")) { wlogdie("Failed to open $inputfile"); }else{ wlog("Opened $inputfile for reading"); } while ($row = fgetcsv($inputfp, 1000, ",")){ $trimrow = trim($row, " "); $count++; writeLine($outfilefp, $trimrow); } if($count !== '0') { wlog("finished the import with a total of $count records"); }else{ wlog("Failed to import data"); } ?> Link to comment https://forums.phpfreaks.com/topic/46475-solved-using-trim-function-to-remove-spaces-in-a-line/ Share on other sites More sharing options...
Barand Posted April 10, 2007 Share Posted April 10, 2007 try <?php while ($row = fgetcsv($inputfp, 1000, ",")){ foreach ($row as $k=>$v) { $row[$k] = trim($v); } $count++; fputcsv($outfilefp, $row); } ?> Link to comment https://forums.phpfreaks.com/topic/46475-solved-using-trim-function-to-remove-spaces-in-a-line/#findComment-226099 Share on other sites More sharing options...
jschofield Posted April 10, 2007 Author Share Posted April 10, 2007 Thanks Barand. Got it going now. THanks Link to comment https://forums.phpfreaks.com/topic/46475-solved-using-trim-function-to-remove-spaces-in-a-line/#findComment-226109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.