Page 1 of 1

detecting lines without anything on them

Posted: Mon Sep 05, 2005 2:39 pm
by jaymoore_299
if I have a file that contains something like this

"
text text text
(possibly some spaces here)
text text text
text text text
text text text
(no spaces)
text text text text text text
(no spaces)
text text text
"

and I save the file as an array:
$contents = file("document.txt");

now do I make a new array that does not include any of the blank lines?

Posted: Mon Sep 05, 2005 2:44 pm
by josh
use fgets() to build your array, and check for blanklines as you're building it

and file() doesn't save the file, it reads it.. from your wording it sounds like you are trying to save the file with it.

Posted: Mon Sep 05, 2005 3:36 pm
by John Cartwright

Code: Select all

$file = file('document.txt');
$empty = array();

for ($x=0; $x <= count($file); $x++) {
  if (!empty($file[$x])) 
    echo $file[$x];
  else
    $empty[] = $x;
} 

foreach ($empty as $line) { 
  echo 'line #'.$line.' was empty <br />';
}
That should give you some ideas