Welcome Guest, Not a member yet? Register   Sign In
Keeping text file newlines with read_file()
#1

[eluser]Mr Lazy[/eluser]
Hi,

It seems that the function read_file() strips out all the newlines from a text file when it is creating the string that it returns. Can anyone suggest a work-around to keep them, as my customer wants to see the text file as it is displayed in the text file, but within a web page.

All suggestions welcome,

Thanks,
L.
#2

[eluser]Dam1an[/eluser]
I'm not sure if read_file actually strips out the new line characters as such, but you need to convert them to '<br />' strings instead
The easiest way is to wrap the data when you output it in the nl2br function

Another option is to use file() which returns an array which you can then implode with the <br /> string
Code:
$lines = file('path/to/file.txt');
echo implode('<br />', $lines);
#3

[eluser]TWP Marketing[/eluser]
Dam1an,
Is file() a php function or is it a CI function? I can't find a reference to it in the php docs. Thanks
#4

[eluser]Dam1an[/eluser]
It's a PHP function
#5

[eluser]TWP Marketing[/eluser]
Got it, thanks. Wish I'd thought of this a month ago, would have saved some time that I now get to spend adjusting my code... sigh.
#6

[eluser]Mr Lazy[/eluser]
Thanks Dam1an, I used the CI function nl2br_except_pre() and it worked brilliantly. I had tried it before I posted but I must have had a mistake in my code because it didn't work, which is why I thought the newlines were being stripped out.

Thanks again...




Theme © iAndrew 2016 - Forum software by © MyBB