Jump to content

Replace empty line with a <br> tag from a php read text file...


Recommended Posts

Is there a way i can read a text file and add a br tag where ever there is an empty line.

 

for example;

 

at the moment i have a text file which says

 

"hello 123 456

 

world abc xyz dfdfd

 

dfdf"

 

there is a blank line between the hello and world.  I need a way to add a br tag in between the 2 lines so when it displays it displays exactly as it is in the text file.

 

currently when i print out the contents of the file, it ignores the line breaks.

 

 

so to sum up,

- read an external text file line by line

- replace or add a br tag where ever PHP finds an empty line.

 

thanks

So, did you actually try the suggested solution before you decided it did not work?

 

oops! i have tried it now and it works fine. I used the follwing code.

 

<?php

 

$file = "line_break.txt";

$handle = fopen($file, "r");

 

$string = fread($handle, filesize($file));

 

echo nl2br($string);

 

?>

 

.. and it works exactly how i want.

 

thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.