Solved

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

Posted on 2014-01-28
5
198 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 54

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 109

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 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