Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedding plain text from a text file, inside of my html document.

Status
Not open for further replies.

jxfish2

Technical User
Jan 24, 2002
183
US
I'm trying to display the contents of a plain text unix file inside of my html document...

I don't want to create a link to the text file, I want the latest data in the text file to be displayed inside of the html / php document anytime the html / php document is refreshed...

Any help would be greatly appreciated...

Joe
 
I believe the answer to my question is this:

read the file into an array, via PHP, then print the
array...

My only problem with this is: I don't know enough about PHP yet, to know how to read the data into the array...

This is NOT the file that I want to read into the array, but for this example, it will suffice:

Let's say that I wanted to read a character-delimited file, such as the /etc/password file, into an array...

Can anyone give me an example of how I would read and parse the /etc/password file into my array?

Thanks in advance,

Joe
 
You could just require("yourtextfile.txt") or include("yourtextfile.txt"). Thats the easiest way. This limtis you formatting though (unless the file is preformatted with html). -gerrygerry
Go To
 
I tried each of the following, and it DID display the file inside of the html document:


include '/tmp/form.out';


$FILE = include '/tmp/form.out';
print $FILE


$fd = fopen ("/tmp/form.out", "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
echo $buffer;
}
fclose ($fd);


However, in each case, it keeps placing one line right after another...

None of these commands seems to recognize the EOL function...

Instead of placing each unique line, on separate lines, they're appended to each other...

I've tried to insert the EOL character "\n", but I'm not sure where to put it, and every place that I've tried so far, has given me an error message...

Any help would be much appreciated...

TIA

Joe
 
Hey!!! I got it!!!

Any one of the above examples works, with one small addition:

The PHP code must be enclosed inside <pre> ... </pre> HTML tags...

EXAMPLE:

<pre>
<?php
include '/tmp/form.out';
?>
</pre>

IT WORKS!!!
 
Now... To figure out arrays, and how to parse &quot;special character delimited&quot; files, placing them into an array...

Any help, especially examples, would be greatly appreciated...

Joe
 
To read a file into an array, you use the file function.
For example
$array = file(&quot;/tmp/form.out&quot;);
Then to parse the file, you just use something like
while (list(,$line) = each($array))
{
$array2 = explode(&quot;:&quot;, $line); //If : is the special character
//Do something with the array here
}
or
foreach ($array as $a)
{
$array2 = explode(&quot;:&quot;, $a); //If : is the special character
//Do something with the array here
} //Daniel
 
Thanks Daniel,

Have a great day...

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top