Display a text file on a web page – PHP

  php

Q(Question):

I have a text file that I am trying to display on a web page. If I cat
or more the file it formats and displays fine. When it comes up in the
browser it seems to loose tabs and the format gets messed up. This is
how I display the file.

$show = file("./fields/combined/$cdp$store");
$arrayitems = sizeof($show);
$x=0;
while ($x < $arrayitems) {
print("$show[$x]\n");
$x++;
}

If I edit the file it has ^M at the end of each line if it matters.
Does anyone have a better idea as to how to display it?

A(Answer):

On Sat, 11 Oct 2003 10:46:20 -0700, Paul wrote:

I have a text file that I am trying to display on a web page. If I cat or
more the file it formats and displays fine. When it comes up in the
browser it seems to loose tabs and the format gets messed up. This is how
I display the file.

$show = file("./fields/combined/$cdp$store"); $arrayitems =
sizeof($show);
$x=0;
while ($x < $arrayitems) {
print("$show[$x]\n");
$x++;
}
}
If I edit the file it has ^M at the end of each line if it matters. Does
anyone have a better idea as to how to display it?

<?php
echo "<pre>\n";
@readfile("./fields/combined/$cdp$store");
echo "</pre>\n";
?>
HTH =)

Regards,

Ian


Ian.H [Design & Development]
digiServ Network – Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.

A(Answer):

Hello Paul,

Paul wrote:

I have a text file that I am trying to display on a web page. If I cat
or more the file it formats and displays fine. When it comes up in the
browser it seems to loose tabs and the format gets messed up. This is
how I display the file.

A browser ignores newlines in the source when rendering HTML.
To get a new line, you can use <BR>.

About the tabs, browsers ignore them too.
You could output table rows for each line in the file
and put what’s between two tabs in a table cell.
Then the <BR> is not necessary anymore also 🙂

Best regards,

Eric

A(Answer):

Eric Veltman wrote:

A browser ignores newlines in the source when rendering HTML.
To get a new line, you can use <BR>.

About the tabs, browsers ignore them too.
You could output table rows for each line in the file
and put what’s between two tabs in a table cell.
Then the <BR> is not necessary anymore also 🙂

Sorry … If the file is a free-format text file and not
a file with fields separated by tabs, then Ian’s suggestion
is much better 🙂

Best regards,

Eric

A(Answer):

Eric Veltman <eric@[RemoveThis]veltman.nu> wrote in message news:<vo************@corp.supernews.com>…

Eric Veltman wrote:

A browser ignores newlines in the source when rendering HTML.
To get a new line, you can use <BR>.

About the tabs, browsers ignore them too.
You could output table rows for each line in the file
and put what’s between two tabs in a table cell.
Then the <BR> is not necessary anymore also 🙂

Sorry … If the file is a free-format text file and not
a file with fields separated by tabs, then Ian’s suggestion
is much better 🙂

Best regards,

Eric

Actually I don’t see any message from Ian about this.
The file is more like a free form document. What about the idea of
maybe trying to read the file and convert Tabs to spaces before I
display it. Does that sound possible? It would take a couple second
longer to display but at least it would look right? I think?

A(Answer):

well , you can use <pre> tag to keep your format , like this:

$show = file("./fields/combined/$cdp$store");
$arrayitems = sizeof($show);
$x=0;
print("<pre>");
while ($x < $arrayitems) { print("$show[$x]\n");
$x++;
}
print("</pre>");
pk****@yahoo.com (Paul) wrote in message news:<4c*************************@posting.google.c om>… I have a text file that I am trying to display on a web page. If I cat
or more the file it formats and displays fine. When it comes up in the
browser it seems to loose tabs and the format gets messed up. This is
how I display the file.

$show = file("./fields/combined/$cdp$store");
$arrayitems = sizeof($show);
$x=0;
while ($x < $arrayitems) {
print("$show[$x]\n");
$x++;
}

If I edit the file it has ^M at the end of each line if it matters.
Does anyone have a better idea as to how to display it?

A(Answer):

Hello Paul,

Paul wrote:

Eric Veltman <eric@[RemoveThis]veltman.nu> wrote in message

Sorry … If the file is a free-format text file and not
a file with fields separated by tabs, then Ian’s suggestion
is much better 🙂

Actually I don’t see any message from Ian about this.
The file is more like a free form document. What about the idea of
maybe trying to read the file and convert Tabs to spaces before I
display it. Does that sound possible? It would take a couple second
longer to display but at least it would look right? I think?

I guess my post arrived at your news server before Ian’s post did.
Perhaps you can see Ian’s post now ? Anyway, he was talking about
using the <pre>pre-formatted text</pre> trick to let the browser
display the pre-formatted text. That’ll work too and is a lot easier.

Converting tabs to spaces could work also, but then you have to
choose the correct number of spaces to replace the tabs and you
will probably want to use a non-proportional font like Courier.

Best regards,

Eric

LEAVE A COMMENT