Michdd Posted June 13, 2009 Share Posted June 13, 2009 I'm returning text from a database and trying to put it inside of a textarea and displaying the line breaks within it. I tried something like: $row['text'] = str_replace('<br />', '\n', nl2br($row['text'])); But I just get 1 line break, then a bunch of \n's display inside the text area. Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/ Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 so the text from your database already ends in \n, you're nl2bring it, only to turn around and replace the br's with \n's? Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-854996 Share on other sites More sharing options...
MadTechie Posted June 13, 2009 Share Posted June 13, 2009 That code doesn't make much sense! convert newline to Break, then convert break to newline! does $row['text'] have the \n in ? try echo "<textarea>{$row['text']}</textarea>"; and post back what you get Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-854998 Share on other sites More sharing options...
Michdd Posted June 13, 2009 Author Share Posted June 13, 2009 Oh whoops. My text already contained line breaks (<br />) even so when I try: str_replace('<br />', '\n', $row['post']) I still get the \n's in the text area. Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-855007 Share on other sites More sharing options...
MadTechie Posted June 13, 2009 Share Posted June 13, 2009 try echo "<textarea>{$row['text']}</textarea>"; and post back what you get Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-855012 Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 in your str_replace try changing '\n' to "\n" (use double quotes instead of single quotes) Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-855014 Share on other sites More sharing options...
Michdd Posted June 13, 2009 Author Share Posted June 13, 2009 in your str_replace try changing '\n' to "\n" (use double quotes instead of single quotes) That worked, but I'm still confused as to why.. Any explanation? Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-855020 Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 double quotes tell php to parse the special chars inside the quotes. So you can use escaped chars that represent things like newline etc.. and php will parse it as a newline. Or tell php to recognize $blah as a variable to parse, rather than literally $blah. php parses stuff in single quotes literally. So with single quotes, you were telling it to replace it with a literal \n instead of a newline. Quote Link to comment https://forums.phpfreaks.com/topic/162036-solved-multiple-line-breaks-textarea/#findComment-855023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.