Solved

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

Posted on 2014-01-28
5
290 Views
Last Modified: 2016-05-17
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
Comment
Question by:ramesh4046
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 39817125
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
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 39817189
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
 
LVL 9

Expert Comment

by:rinfo
ID: 39817350
myabel you should consider changing
$line1 =  ltrim($line);
to $line1= trim(nl2br($line);
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39817376
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

729 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