mits Posted May 12, 2010 Share Posted May 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 12, 2010 Share Posted May 12, 2010 http://us2.php.net/nl2br Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/#findComment-1057403 Share on other sites More sharing options...
mits Posted May 12, 2010 Author Share Posted May 12, 2010 http://us2.php.net/nl2br that isn't going to work for what i want. That assumes i have a "\n" for all line breaks. That just replaces \n or \r with the br tag. the text file has no html coding....it's a text file with plain text. Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/#findComment-1057408 Share on other sites More sharing options...
Cagecrawler Posted May 12, 2010 Share Posted May 12, 2010 \n or \r (or \n\r) is what the OS uses to define a line break. They are there in a plain text file, you just can't see them. Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/#findComment-1057410 Share on other sites More sharing options...
PFMaBiSmAd Posted May 12, 2010 Share Posted May 12, 2010 So, did you actually try the suggested solution before you decided it did not work? Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/#findComment-1057431 Share on other sites More sharing options...
mits Posted May 13, 2010 Author Share Posted May 13, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/201550-replace-empty-line-with-a-tag-from-a-php-read-text-file/#findComment-1057474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.