Preserving user-entered linebreaks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Glowing Face Man
Forum Newbie
Posts: 7
Joined: Fri Oct 16, 2009 3:56 pm

Preserving user-entered linebreaks

Post 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 :P

What's the best way to settle this issue?

Thanks :D :mrgreen: :P
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Preserving user-entered linebreaks

Post 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
Glowing Face Man
Forum Newbie
Posts: 7
Joined: Fri Oct 16, 2009 3:56 pm

Re: Preserving user-entered linebreaks

Post by Glowing Face Man »

Hah, I should've guessed..... is there anything PHP doesn't have a built-in function for??? :) Thanks a lot
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Preserving user-entered linebreaks

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Preserving user-entered linebreaks

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Preserving user-entered linebreaks

Post 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 :P

What's the best way to settle this issue?

Thanks :D :mrgreen: :P
Well if you're really displaying text and not HTML then wrap all of the text in <pre></pre> not just the newlines.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply