views:

103

answers:

3

I input some text in <textarea></textarea>, I enter some line breaks, spaces,etc. purposely. However, after the text is submitted and rendered within <pre></pre>, the newline, whitespaces are gone, they are replaced by something like \r\n\r\n, what's wrong? How can I keep the original format?

 <textarea name="detail" id="detail" rows="7" cols="60" style="font-family:Arial, Helvetica, sans-serif"><?php echo $detail ?></textarea>

I render the text in HTML.

This is the output:

The urban agglomeration of the Hangzhou metropolitan area (杭州市区) has a resident population of 3.9319 million as of 2003, of which 2.6367 million ...\\\\r\\\\nHangzhou is located in northern Zhejiang province, eastern China, at the southern end of the Grand Canal of China, on the plain of the mid ...\\\\r\\\\nIn late summer (August to September), Hangzhou, along with other cities in Zhejiang province, suffer typhoon storms, but typhoons seldom strike it directly. Generally. Edited\r\n\r\nAnother line.

The data-type is varchar(MySQL), does it have anything with the problem?

+1  A: 

PHP

http://stackoverflow.com/questions/1897452/allow-html-in-textarea

asp.net

<%@ Page ValidateRequest="false"
Saar
A: 

You need to run your output through the nl2br function first:

<pre><?php echo nl2br($content) ?></pre>

Make sure you escape the data to protect against nasty input at some point too - either before you stick it in the database using mysql_real_escape_string or when you print it to the screen using htmlspecialchars

Dexter
The data doesn't have new lines in it, it has something which resolves to the literal sequence `\r\n`, presumably there is some double escaping going on down the line.
David Dorward
The literal sequence \r\n _is_ a newline sequence - example #2 on http://ca2.php.net/nl2br describes *exactly* this situation, whereby \r\n literals are replaced with <br/> tags.
Dexter
yes, double escaping.
Steven
A: 

Are you entering these into a web app? One you wrote? What's probably happening is they're being recognised as new lines. On Windows platforms, a paragraph/newline is done with a \r (carriage return) and \n (new line). If you're then redisplaying them on a new page or site, you'll need to sub back in some html, or else in the original submit, replace \r\n with <pre>(whitespaceofyourchoice)<\pre>.

\r and \n are ASCII characters and not part of HTML formatting. As such, web browsers will choose to ignore them.

Mark Mayo