Trying to find the first empty line number in my 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
laxmisarva
Forum Newbie
Posts: 2
Joined: Mon Nov 30, 2009 3:24 am

Trying to find the first empty line number in my file

Post by laxmisarva »

I am Trying to find the first empty line number in my file and i used this code this is not working i want some help


$file=file($file_name) ;
foreach($file as $line_no=>$line)
{

if(preg_match('/^$/',$line))
{
$line_no = $i;
echo $line_no;
break;}
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Trying to find the first empty line number in my file

Post by Jonah Bron »

How about:

Code: Select all

$file = file($file_name);
foreach ($file as $line => $content) {
    if (empty($content)) {
        echo $line + 1;
        break;
    }
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trying to find the first empty line number in my file

Post by requinix »

Don't forget that file() will also return the newlines attached, so it's more like

Code: Select all

if (trim($line) == "")
If you're worried about spaces and tabs too (as trim() also removes those),

Code: Select all

if (rtrim($line, "\r\n") == "")
Post Reply