mike23
mike23

Reputation: 1312

How can I prevent line break when reading a CSV text file with php?

I have a CSV text file which contains serval entries of "books", each book is on one line (separator: line break), and each column is separated by a tabulation.

I'm reading the content the file with :

$books = file( "/path/to/file/mycsv.txt" , FILE_SKIP_EMPTY_LINES ); 

That way I can go throuhg each element of the "books" array and read my individual "book" data.

The problem is that each book has a "description" column, which also contains line breaks. On import, this messes up the data, as it creates a line break before the end of an item.

How can I prevent that ?

Upvotes: 1

Views: 1367

Answers (2)

Femi
Femi

Reputation: 64700

Does your file include quotes around the description column, because if it does I believe fgetcsv handles that situation.

Upvotes: 3

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

You can use PHP RTRIM to trim the line breaks. or you can use a inbuilt feature of php fgetcsv to fetch the file.

Upvotes: 1

Related Questions