Page 1 of 1
Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 1:52 am
by Glowing Face Man
Hi, I'm building a web 2.0 thingy from scratch and I wanna display text which users input. It's all good so far, except that all the linebreaks are destroyed, so no matter how carefully they insert whitespace, their input gets displayed as one big block of text.
The data is submitted (method="post") through a textarea. The mysql_real_escape version is stored in mysql. Then it's read from the database (no unescaping necessary) and passed through htmlentities before being printed into the body of the webpage.
I tried replacing htmlentities with the following function:
Code: Select all
function brentities($x)
{
$x = str_replace("[LINEBREAK]","\n\r",$x);
$x = str_replace("[LINEBREAK]","\r\n",$x);
$x = str_replace("[LINEBREAK]","\n",$x);
$x = htmlentities($x);
$x = str_replace("<pre>\n</pre>","[LINEBREAK]",$x);
return $x;
}
But this doesn't seem to work. I also tried setting "white-space: pre;" in the CSS for the appropriate div, but that caused the text to bleed outside the div (and even outside the screen), so no go
What's the best way to settle this issue?
Thanks

Re: Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 2:47 am
by Mark Baker
don't faff about with overcomplicated logic that doesn't work.
Read the text from the database, then echo nl2br($text) when you echo it out
Re: Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 10:19 am
by Glowing Face Man
Hah, I should've guessed..... is there anything PHP
doesn't have a built-in function for???

Thanks a lot
Re: Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 10:36 am
by markusn00b
Glowing Face Man wrote:Hah, I should've guessed..... is there anything PHP
doesn't have a built-in function for???

Thanks a lot
It won't make my coffee in the morning.. lord knows I've tried.
Re: Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 10:55 am
by Mark Baker
markusn00b wrote:Glowing Face Man wrote:Hah, I should've guessed..... is there anything PHP
doesn't have a built-in function for???

Thanks a lot
It won't make my coffee in the morning.. lord knows I've tried.
You mean you haven't found the alarmCall class yet?
Code: Select all
$wakeup = new alarmCall();
$wakeup->setTimezone('Europe/London');
$wakeup->setAlarm('06:45');
$wakeup->setDrink('Coffee')
$wakeup->getDrink()->blend('Blue Mountain');
$wakeup->getDrink()->milk(true);
$wakeup->getDrink()->sugar(2);
$wakeup->getDrink()->size(wakeup::MUG);
$wakeup->go();
Unfortunately, apache always times out after 5 minutes, so I've never seen how strong it makes the coffee
Re: Preserving user-entered linebreaks
Posted: Thu Oct 22, 2009 11:46 am
by AbraCadaver
Glowing Face Man wrote:Hi, I'm building a web 2.0 thingy from scratch and I wanna display text which users input. It's all good so far, except that all the linebreaks are destroyed, so no matter how carefully they insert whitespace, their input gets displayed as one big block of text.
The data is submitted (method="post") through a textarea. The mysql_real_escape version is stored in mysql. Then it's read from the database (no unescaping necessary) and passed through htmlentities before being printed into the body of the webpage.
I tried replacing htmlentities with the following function:
Code: Select all
function brentities($x)
{
$x = str_replace("[LINEBREAK]","\n\r",$x);
$x = str_replace("[LINEBREAK]","\r\n",$x);
$x = str_replace("[LINEBREAK]","\n",$x);
$x = htmlentities($x);
$x = str_replace("<pre>\n</pre>","[LINEBREAK]",$x);
return $x;
}
But this doesn't seem to work. I also tried setting "white-space: pre;" in the CSS for the appropriate div, but that caused the text to bleed outside the div (and even outside the screen), so no go
What's the best way to settle this issue?
Thanks

Well if you're really displaying text and not HTML then wrap all of the text in <pre></pre> not just the newlines.