parse every other line

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
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

parse every other line

Post by Daisy Cutter »

I have this code that I am using to display some quotes on a page:

Code: Select all

<ul id="quotesul">
<?php
$file = "quotes.txt";
$Array = file($file);
foreach ($Array as $key => $val) {
echo "<li>";
echo $val;
echo "</li>";
} 
?>
</ul>
quotes.txt is done for every line right now, but it looks very messy. I want to make it easier to read by putting a space in between every quote, but then of course I get empty <li>'s.

I want to know how to parse every other line (starting with 1) so that it skips the empty lines. I know that might not be the only way, another might be just ignoring lines that contain only EOF or something. Ideas?

Thanks. :wink:
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: parse every other line

Post by neophyte »

how bout this?

Code: Select all

<ul id="quotesul">
<?php
$file = "quotes.txt";
$Array = file($file);
foreach ($Array as $key => $val) {
if(!empty($val)){
 echo "<li>";
       echo $val;
  echo "</li>";
}
} 
?>
</ul>
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

neophyte you led me in the right direction
your fix didn't work but this did:

Code: Select all

<ul id="quotesul"> 
<?php 
$file = "quotes.txt"; 
$Array = file($file); 
foreach ($Array as $key => $val) { 
if($val!=="\n"){ 
echo "<li>"; 
echo $val; 
echo "</li>"; 
} } 
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Glad you found a solution. :D
Post Reply