March 31, 2012

Data retrieved from database ignoring line breaks

Question by JimmyBanks

Data that is stored in my mysql database, has the breaks of text input properly, but when retrieved the data no longer has the breaks, and everything is displayed as one string without any <br>. Any idea why this would occur?

The data column is the format text,
for example: in a table there is:

hey
how
do
you
do

After retrieving the data ill echo $mesarray[0][message]; and the result is:

hey how do you do

I want the line breaks to appear, but they dont.

Answer by Starx

The reason is because HTML does not understand line breaks. They need <br /> tags to break lines.

There is a function called nl2br() which can be used to convert new lines to <br>

echo nl2br($mesarray[0][message]);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!