Jump to content


Photo

replacing "\n" with <br />


  • Please log in to reply
4 replies to this topic

#1 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 04 April 2006 - 08:14 PM

Not sure if i'm doing this wrong or if i'm just using the wrong function. I have a very simple guestbook setup for our wedding site. It accepts plain text entries via a textarea field and retains them in a MySQL database. When i echo those back to the web site i want to replace all the user entered carriage returns (\n etc) with <br /> tags before it's printed to the page. This is how i have it so far (not working at all)...

$message = str_replace('\n','<br />',$row['message']);

echo '<div>'.$message.'</div>';

1) why isn't that working?
2) are line breaks specific to the server they're submitted to or the browser they're submitted from? For example, if it's server specific and on a unix server i'm guessing "\n" is correct, but if it's client/browser specific then i'd have to escape "^m" for mac "\n" for unix, "\r\n" for windows etc. right?

Anyone up for bringing this down to my level? :)

#2 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 04 April 2006 - 08:24 PM

Have you tried this yet:
$something = nl2br($something);


#3 bcamp1973

bcamp1973
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 04 April 2006 - 08:31 PM

[!--quoteo(post=361714:date=Apr 4 2006, 04:24 PM:name=cmgmyr)--][div class=\'quotetop\']QUOTE(cmgmyr @ Apr 4 2006, 04:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Have you tried this yet:
$something = nl2br($something);
[/quote]

didn't even know about that one...duh...thanks, that should work perfectly.

#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 04 April 2006 - 09:27 PM

Wish they would do BR2NL()

That would be helpfull...


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 April 2006 - 09:56 PM

Since the function nl2br() adds the string "<br />" to the string, it doesn't replace the newline character, the function br2nl() would simply be a str_replace('<br />','',$target_string).

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users