detecting lines without anything on them

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
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

detecting lines without anything on them

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Post Reply