Page 1 of 1

Trying to find the first empty line number in my file

Posted: Tue Sep 21, 2010 2:46 pm
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;}
}

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

Posted: Tue Sep 21, 2010 2:49 pm
by Jonah Bron
How about:

Code: Select all

$file = file($file_name);
foreach ($file as $line => $content) {
    if (empty($content)) {
        echo $line + 1;
        break;
    }
}

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

Posted: Wed Sep 22, 2010 12:22 am
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") == "")