[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

php - eliminate the space in the beginning while writing to CSV file

I am having an array in PHP which contains some line breaks (\n) also as the array elements. I am writing these array elements into the CSV file using fwrite statement as below.

$newLine[] = $row[$i].",";
$newLine[] = "\n";
$csv2 [] = implode(" ", $newLine);
$file1 = fopen("/home/huadong/public_html/ramesh/output_updated.csv","w");
foreach ($csv2 as $line):
$line1 =  ltrim($line);
fwrite($file1, $line1 . PHP_EOL);
endforeach;
fclose($file1);

Open in new window


ltrim is not working as it trims only leading white spaces. I have to replace the extra space in the beginning of the CSV file while it is getting written.

The CSV file is getting written as below.

Jack 1234
 John 3456
 Jason 3321

Open in new window


I am expecting to write into the CSV file as below.

   Jack 1234
   John 3456
   Jason 3321

Open in new window

0
ramesh4046
Asked:
ramesh4046
2 Solutions
 
Marco GasiFreelancerCommented:
Seeing your last two snippets seems you want leave left blank spaces and delete right black spaces: if so try using rtrim()

If you use ltrim, it deletes left blank space rtrim() deletes right blank spaces and trim deletes both left and right spaces.

I i didn't understand correctly the problem, please explain one more time :)
0
 
Julian HansenCommented:
It is difficult to understand what you are trying to do - there are some confusing aspects to your code. I have added comments in BOLD to highlight them - please answer the questions posed below - it will help us understand better what you are trying to achieve.
$newLine[] = $row[$i].","; // WHERE IS $row commingfrom?
// WHY ARE YOU DOING THIS IN THIS WAY?
$newLine[] = "\n";
$csv2 [] = implode(" ", $newLine);
$file1 = fopen("/home/huadong/public_html/ramesh/output_updated.csv","w");
// WHY FOREACH? IMPLODE CREATES A STRING?
foreach ($csv2 as $line):
$line1 =  ltrim($line);
// YOU ADDED A \n TO THE ARRAY BUT NOW YOU ARE USING PHP_EOL
fwrite($file1, $line1 . PHP_EOL);
endforeach;
fclose($file1);

Open in new window

Line 1 of your code you are appending a comma to the line but it is not show in the sample output you gave us.
0
 
rinfoCommented:
myabel you should consider changing
$line1 =  ltrim($line);
to $line1= trim(nl2br($line);
0
 
Ray PaseurCommented:
The right way to ask this question is to show us your test data and show us the output you want to get from it.  If you do that we can give you a tested and working code sample.  Asking that way eliminates any need for speculation (and we really do not need to see code that doesn't work).

If your goal is a CSV file, PHP FputCSV() is a function you want to know about.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now