f



Display a file line by line but not blank lines.

Sorry I just can't work how not to display balnk lines with in a file
to the web, especially at the end of the file.

$contents = fopen("$file",'rb');

if (!$contents)
{
  echo '<strong>Please Try Again Later.</strong><br />';
  exit;
}

$counter = 0;
while (!feof($contents))
{
    $counter = $counter + 1;
    $line = fgets($contents, 999);
    echo $counter.'. ';
    echo $line.'<br /><br />';
}
0
4/4/2010 7:14:07 PM
comp.lang.php 32646 articles. 0 followers. Post Follow

6 Replies
1114 Views

Similar Articles

[PageSpeed] 12

Rob B wrote:
> Sorry I just can't work how not to display balnk lines with in a file
> to the web, especially at the end of the file.
> 
> $contents = fopen("$file",'rb');
> 
> if (!$contents)
> {
>   echo '<strong>Please Try Again Later.</strong><br />';
>   exit;
> }
> 
> $counter = 0;
> while (!feof($contents))
> {
>     $counter = $counter + 1;
>     $line = fgets($contents, 999);
>     echo $counter.'. ';
>     echo $line.'<br /><br />';
> }

Just test to see if the line is empty or not, i.e.

if (trim($line) <> '') {
   echo $counter . '. ' . $line . '<br /> <br />';

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
Jerry
4/4/2010 7:49:19 PM
On 4 Apr, 20:49, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> Rob B wrote:
> > Sorry I just can't work how not to display balnk lines with in a file
> > to the web, especially at the end of the file.
>
> > $contents =3D fopen("$file",'rb');
>
> > if (!$contents)
> > {
> > =A0 echo '<strong>Please Try Again Later.</strong><br />';
> > =A0 exit;
> > }
>
> > $counter =3D 0;
> > while (!feof($contents))
> > {
> > =A0 =A0 $counter =3D $counter + 1;
> > =A0 =A0 $line =3D fgets($contents, 999);
> > =A0 =A0 echo $counter.'. ';
> > =A0 =A0 echo $line.'<br /><br />';
> > }
>
> Just test to see if the line is empty or not, i.e.
>
> if (trim($line) <> '') {
> =A0 =A0echo $counter . '. ' . $line . '<br /> <br />';
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D- Hide quoted text =
-
>
> - Show quoted text -

Tried something like that, but the script hangs, I think I'm missing
something somewhere.
0
Rob
4/4/2010 8:26:38 PM
On Sun, 4 Apr 2010 13:26:38 -0700 (PDT), Rob B wrote:

> On 4 Apr, 20:49, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Rob B wrote:
>>> Sorry I just can't work how not to display balnk lines with in a file
>>> to the web, especially at the end of the file.
>>
>>> $contents = fopen("$file",'rb');
>>
>>> if (!$contents)
>>> {
>>> � echo '<strong>Please Try Again Later.</strong><br />';
>>> � exit;
>>> }
>>
>>> $counter = 0;
>>> while (!feof($contents))
>>> {
>>> � � $counter = $counter + 1;
>>> � � $line = fgets($contents, 999);
>>> � � echo $counter.'. ';
>>> � � echo $line.'<br /><br />';
>>> }
>>
>> Just test to see if the line is empty or not, i.e.
>>
>> if (trim($line) <> '') {
>> � �echo $counter . '. ' . $line . '<br /> <br />';
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================- Hide quoted text -
>>
>> - Show quoted text -
> 
> Tried something like that, but the script hangs, I think I'm missing
> something somewhere.

<> should be !=
0
richard
4/5/2010 12:07:34 AM
In article <118x3hwkjsn9o$.13b2cm8w40d0v$.dlg@40tude.net>, member@newsguy.com wrote:
>On Sun, 4 Apr 2010 13:26:38 -0700 (PDT), Rob B wrote:
>
>> On 4 Apr, 20:49, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>>> Rob B wrote:
>>>> Sorry I just can't work how not to display balnk lines with in a file
>>>> to the web, especially at the end of the file.
>>>
>>>> $contents = fopen("$file",'rb');
>>>
>>>> if (!$contents)
>>>> {
>>>> � echo '<strong>Please Try Again Later.</strong><br />';
>>>> � exit;
>>>> }
>>>
>>>> $counter = 0;
>>>> while (!feof($contents))
>>>> {
>>>> � � $counter = $counter + 1;
>>>> � � $line = fgets($contents, 999);
>>>> � � echo $counter.'. ';
>>>> � � echo $line.'<br /><br />';
>>>> }
>>>
>>> Just test to see if the line is empty or not, i.e.
>>>
>>> if (trim($line) <> '') {
>>> � �echo $counter . '. ' . $line . '<br /> <br />';
>> 
>> Tried something like that, but the script hangs, I think I'm missing
>> something somewhere.
>
><> should be !=

The two are synonymous in PHP.

Of course, since the OP didn't tell us what he tried, or what happened when he 
tried it, it's not possible to say what *is* wrong. It is possible, however, 
to say definitely that using <> instead of != is *not* what's wrong.

Please go back to lurking, Richard.
0
spambait
4/5/2010 12:18:24 AM
Rob B wrote:
> On 4 Apr, 20:49, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Rob B wrote:
>>> Sorry I just can't work how not to display balnk lines with in a file
>>> to the web, especially at the end of the file.
>>> $contents = fopen("$file",'rb');
>>> if (!$contents)
>>> {
>>>   echo '<strong>Please Try Again Later.</strong><br />';
>>>   exit;
>>> }
>>> $counter = 0;
>>> while (!feof($contents))
>>> {
>>>     $counter = $counter + 1;
>>>     $line = fgets($contents, 999);
>>>     echo $counter.'. ';
>>>     echo $line.'<br /><br />';
>>> }
>> Just test to see if the line is empty or not, i.e.
>>
>> if (trim($line) <> '') {
>>    echo $counter . '. ' . $line . '<br /> <br />';
>>
>> - Show quoted text -
> 
> Tried something like that, but the script hangs, I think I'm missing
> something somewhere.

"something like that" is not the same as doing that.

My comments were aimed at the code you posted.  If you did something 
else, you didn't post it and I have no idea what you might have done.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
Jerry
4/5/2010 12:25:44 AM
Rob B wrote:

> Sorry I just can't work how not to display balnk lines with in a file
> to the web, especially at the end of the file.
> 
> $contents = fopen("$file",'rb');

  $contents = fopen($file, 'rb');

'b' might be a problem here, see <http://php.net/fopen>

> if (!$contents)

  if ($contents !== false)

> {
>   echo '<strong>Please Try Again Later.</strong><br />';

I do not think words in simple messages should start with a capital letter; 
AFAIK that pattern is limited to headings in English.

>   exit;
> }
> 
> $counter = 0;
> while (!feof($contents))
> {
>     $counter = $counter + 1;

    ++$counter;

>     $line = fgets($contents, 999);

    if (preg_match('/\S/', $line))
    {
      /* line is not blank */

      /* It's not XHTML, is it? */
      echo "{$counter}. {$line}<br>\n";
    }

> }


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
0
Thomas
4/6/2010 11:13:20 PM
Reply: