简体   繁体   中英

nl2br() not working quite right

I am using the following to submit a post from a textarea to my mysql database in order to preserve single/double/triple line spacing on the output.:

$update = nl2br(htmlentities($string, ENT_QUOTES, 'UTF-8'));

Sometimes it works but sometimes a double is replaced with a single line space and other times double line spaces are preserved. Here is my output, all of this was input using double line spaces (hitting enter twice). Any suggestions on how to improve this would be appreciated:

    something

    went

    a 
    little

    wrong
    there

Thanks Prinzhorn. Below is the CREATE TABLE statement for the table in question.

CREATE TABLE IF NOT EXISTS `ticket_updates` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ticket_id` varchar(100) NOT NULL,
  `update` text NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `member_id` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;

If You use htmlentities() it encodes "\\n" to "%5Cn". so, it might get create problem to get the actual output.

Try the following code, might helps you to find, what the actual output you need.

echo htmlentities($string, ENT_QUOTES, 'UTF-8');

My issue was caused by using wordwrap() to try and format the output and NOT due to:

nl2br(htmlentities($string, ENT_QUOTES, 'UTF-8'));

I changed my wordwrap($update, 50, " \\n", true) to CSS3 propertey word-break:break-all; ( word-break:break-word; would not work with IE9)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM