/    Sign up×
Community /Pin to ProfileBookmark

Problem with reading .csv file.

Hi,

I am trying to read data from excel files and then import the data to the database. The problem I am having is excel cell contain line break. It creates new line in the .csv file.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@DasherJun 20.2012 — You could create the file, and then edit out the line breaks where not wanted, or eliminate the line breaks in excel.. Each line in a csv is terminated with a line break.
Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — you could also use $rows = file('filename.csv'); to create an array from each row,

then use $rows = array_map('trim',$rows); to remove all the line breaks, you will be left

with an array named $rows containing all of the rows from the csv file. you can just loop through the $rows array an insert them one at a time at that point, or you can use implode to combine them back into one long string like this $str = implode($rows,"|");

in this case i used pipe characters '|' to seperate them, but you could use whatever you like if you only want to do a single insert

$rows = file('filename.csv'); //create an array from each row in the .csv file

$rows = array_map('trim',$rows); //trim whitespace from the front and back of each row of data

you can use a for(){} or foreach(){} loop to go through the array

$str = implode($rows,"|"); //optionally convert the array back into one long string seperated by pipe chars
Copy linkTweet thisAlerts:
@NogDogJun 20.2012 — If it is a properly formatted CSV file and if you are using fgetcsv() to parse it, newlines within a field should not be a problem.
Copy linkTweet thisAlerts:
@bsmbahamasJun 20.2012 — thanks nogdog, nevr seen the fgetcsv() function before.

= )
×

Success!

Help @byebyebyezzz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...