Check a blank line while reading a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kb0000
Forum Newbie
Posts: 4
Joined: Thu May 08, 2008 5:31 am

Check a blank line while reading a file

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Check a blank line while reading a file

Post 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"));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply