Solved

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

Posted on 2014-01-28
5
156 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 30

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now