PDA

View Full Version : Loading text file into HTML?


pininfarina
11-25-2007, 06:10 PM
I'm wondering if it's possible to load the text in a txt file directly into a div just by putting in the link to the txt file or something like that.


anyone know if that can be done?

pininfarina
11-25-2007, 06:14 PM
I believe I might need something like php to get this done, to bad I'm very limited in my php knowledge ( I can only implement code not think it up)

any help on that area?

laxman
11-25-2007, 08:38 PM
Yes, HTML cannot do this, you will need something like PHP. I know there are file read and write functions:

http://www.tizag.com/phpT/fileread.php

so check that out. Read the file into a variable, then just echo the variable where ever you want the text.

bravo2zero
11-25-2007, 11:30 PM
<?php
include_once("textfile.txt");
?>

something like this will do it but I'm not shore how the formating will go you may have to add some <BR> and <P> to your text file for page breaks etc other wise it may just put all the text up and wrap it around .

danlefeb
11-26-2007, 07:35 AM
bravo brings up a good point in that a standard .txt file doesn't have formatting. So importing it will just bring everything in one big clump of words. That means you'll want to format it...and if you're going to be formatting the text into HTML anyway, then you can do something simple like an iFrame and do away with the need for PHP or any server-side script.

bravo2zero
11-26-2007, 08:54 AM
you could use fopen and fread and print one line of text at a time and use a <br> tag in your while loop. that might be the best way to do it with php.

pininfarina
11-26-2007, 05:13 PM
yes yes thnx for that one guys,
isn't it just possible to format whatever text is rendered out of that text file just using css styling on the div where the text is shown or rather where the php code is implemented?

bravo2zero
11-26-2007, 08:31 PM
add this to the top of your page and change the page from .HTML to .php
extention

<?php
$myFile = "events.txt";
$fp = fopen($myFile, 'r');
$content = fread($fp,filesize($myFile));
fclose($fp);
$content = "<p>".$content."</p>";
$content = str_replace(chr(10),"</p><p>",$content);
?>


this will load the file and format it

where you want the text to be displayed you use this bit of code.

<?php echo $content; ?>

leave the HTML as it was originally.

pininfarina
11-27-2007, 07:11 PM
wow thanks so much bravo2zero, I'll implement this code asap

bravo2zero
11-28-2007, 12:51 AM
yeah no worries just put me and dt in the credits page .. lol