Page 1 of 1

Reading File with Line Breaks?

Posted: Sun Oct 26, 2003 1:52 pm
by locell
I'm reading from a file that has line breaks as in spacing items like this:

Item1
ITEM2

ITEM3

But it comes out like Item1ITEM2 ITEM3

Here is the Code:

if (!$file_handle = fopen($file,"r")) { echo "Cannot open file."; }
if (!$file_contents = fread($file_handle, filesize($file))) { echo "Cannot retrieve file contents."; }
else { echo "$file_contents\r\n"; }
fclose($file_handle);

If some one could help me I'd appreciate it a LOT!!!

THANK YOU

Posted: Sun Oct 26, 2003 3:24 pm
by DuFF
If you are trying to output this in HTML you just need to change the \r\n to a <br>.

Instead of

Code: Select all

<?php
 else { echo "$file_contents\r\n"; } 

?>
put

Code: Select all

<?php
 else { echo "$file_contents<br>"; } 
?>

Posted: Sun Oct 26, 2003 3:49 pm
by Gen-ik
This will replace all of the line breaks in the file with <br> tags.

Code: Select all

<?
if (!$file_handle = fopen($file,"r")) { echo "Cannot open file."; } 

if (!$file_contents = fread($file_handle, filesize($file))) { echo "Cannot retrieve file contents."; } 
else { echo nl2br($file_contents); } 
fclose($file_handle); 
?>