Page 1 of 1

Check a blank line while reading a file

Posted: Thu Jun 03, 2010 8:38 am
by kb0000
I want to read a file a.txt

It has contents in the format

This is line 1.

This is line 3. Further in this line.
This is line 4.

This is line 6.

I want to read this file (line by line, i think) and check if the current line is blank (like there is no Line 2 i.e. a blank line.)

What I want to do is that - now in the above text file there are 3 sets of line or 3 paragraphs. I want to check if there is a blank line between two set of lines.
Or is there some way to fetch paragraphs???

I want to separate these 3 sets of line in 3 rows in an excel sheet.
i.e.
In excel sheet columns:
A1 - This is line 1.
A2 -This is line 3. Further in this line.
This is line 4.
A3 -
This is line 6.

How I can do this?

Re: Check a blank line while reading a file

Posted: Thu Jun 03, 2010 9:20 am
by AbraCadaver
It would probably be easiest to explode the file into an array based on double newlines. This should do it:

Code: Select all

$columns = explode(PHP_EOL.PHP_EOL, file_get_contents("/path/to/file.txt"));