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;}
}
Trying to find the first empty line number in my file
Moderator: General Moderators
-
laxmisarva
- Forum Newbie
- Posts: 2
- Joined: Mon Nov 30, 2009 3:24 am
- 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
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
Don't forget that file() will also return the newlines attached, so it's more like
If you're worried about spaces and tabs too (as trim() also removes those),
Code: Select all
if (trim($line) == "")Code: Select all
if (rtrim($line, "\r\n") == "")