Solved

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

Posted on 2014-01-28
5
178 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
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 52

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now