View Full Version : Include Text File with Line Breaks
tomjelfs
05-12-2008, 06:22 PM
Hey everyone
How do I go about including a .txt file and have that text line break every time there is a return in the .txt file?
Any ideas.
Cheers
http://php.net/include
http://php.net/nl2br
tomjelfs
05-13-2008, 03:38 AM
Yeah I know how to include a file and I'm aware of the nl2br command but it doesn't work for me:
<?php
$text = include 'products/prod1.txt';
echo nl2br($text);
?>
That just echos out the text with no line breaks.
Any ideas?
can we see the file?
Seems that you dont have your newlines stored.
tomjelfs
05-13-2008, 09:05 AM
Do you mean the text file or the php file?
Sorry sometimes im kinda blind.
This wont work:
<?php
$text = include 'products/prod1.txt';
echo nl2br($text);
This is from include manual:
return.php
<?php
$var = 'PHP';
return $var;
?>
noreturn.php
<?php
$var = 'PHP';
?>
testreturns.php
<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>
Meaning basically your code includes the file and then uses nl2br() on the return value thats not the string at all.
You need to use
http://php.net/file_get_contents
tomjelfs
05-14-2008, 05:30 AM
Ah great thanks that worked.
End result.
<?php
$info = file_get_contents("products/rapidno1.txt");
echo nl2br($info);
?>
tomjelfs
05-20-2008, 08:04 AM
Ahh bugger!!! I uploaded this last night and hit a snag:
Fatal error: Call to undefined function: file_get_contents() in /var/www/domains/a/c/o/www.acorn-creative.com/public_html/tjs_stuff/vineherbal/aboutus.php on line 30
Apparently file_get_contents() was in php 4 and is no longer supported.
Is there an equivalent in 5?
Horus_Kol
05-20-2008, 08:31 AM
file_get_contents() is in PHP5:
(PHP 4 >= 4.3.0, PHP 5) means all versions of PHP4 since 4.3.0 AND all versions of PHP5
tomjelfs
05-20-2008, 01:53 PM
Ah it would appear my isp server only has 4.2.3 Guess I'll call them tomorrow see what they can do.
Cheers
tomjelfs
05-21-2008, 07:25 AM
Well my isp have been recently aquired by Demon and so as we are yet to be migrated they don't seem to care about upgrading the php version, bloody tossers.
Time to do some rethinking on the website or find new hosting.
Horus_Kol
05-25-2008, 06:09 PM
that's really odd - PHP 5 has been around for years... they're even starting to test PHP 6...
are you sure you don't have PHP 5 on your host? usually you have to specify using .php5 (or you can use .htaccess to set the application)
tomjelfs
05-26-2008, 02:45 AM
Well the company that we host with got bought out by Demon so we are apparently still on the old company's server and we will be migrated across soon. I'll be switching hosting when it rolls around to renewal.
¥åßßå
05-26-2008, 05:16 AM
<?php
echo nl2br( implode( '', file( 'your_file.txt' ) ) );
?>
¥
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.