tags:

views:

170

answers:

5

I'm writing by own blogging engine in PHP with a MYSQL backend database. MY question is: How would you go about making user comments and blog posts include newlines wherever they are appropriate?

For example, if a user hits the return key in the message/comments box how would this translate into a new line that would show in the browser when the comment is viewed?

+7  A: 

PHP has a function: nl2br which turns new lines into <br />

www.php.net/nl2br

Crad
I learnt not to trust functions like that after the PHP developers changed the semantics in a minor patch version for no reason.
Jim
Do you have specifics? I see this function changed at 5.3.0 to add a "is xhtml" bool parameter and at 4.0.5 it was made XHMTL compliant by adding a closing slash to the tag. Or is there a specific function you're referring to?
Crad
I'm talking about the 4.0.5 change. It was totally unnecessary, entirely inappropriate for a minor patch version, and broke code. What were they thinking? It's that kind of cowboy attitude that makes me glad I don't have to write PHP any more.
Jim
Well... 4.0.5 was a long time ago. They seem (to me) to have a much more reasoned development process now. Typically if they do something like that now they add an option to enable it (hence the is xhtml parameter).
Michael Johnson
4.0.5 is a long time ago, but there is other stuff just like that. I get all the issues people have just keep in mind that PHP is a community product. Report bugs, post to mailinglists. You have the power! (I know it sounds like a commercial, but it's really true.)
Till
I can think of quite a few things about the development process, such as the lack of addressing needle haystack/haystack needle ordering while making major revisions.
Crad
+2  A: 

Replace \n\n with </p><p> and then replace \n with <br>.

PS: Pirate day was last week :).

Jim
If you do this, make sure to wrap the entire output with <p> and </p>!
Peter Bailey
Doesn't this suggestion assume that you always preface content with <p>?
Crad
Yes, but given that he's talking about user comments and blog posts, that's not an unreasonable assumption.
Jim
A: 

It's also important what your using for a comment editor. If your using a standard textbox then yes, nl2br is what your looking for. If your going a bit more advanced such as using a WYSIWYG editor like tinyMCE, then it has configuration that can handle that for you.

DreamWerx
+1  A: 

nl2br() (http://php.net/nl2br) is perfectly good, however that Wordpress Guy (Matt Mullenweg) has a really good function, which is a bit more advanced as it converts double line breaks to paragraphs instead (better semantically). You can find it in the Wordpress source code or here: http://ma.tt/scripts/autop/

hellweaver666
Really useful link, thanks!
Philip Morton
A: 

If you happen to need more formatting options (beyond paragraphs), employ something like Text_Wiki or PHP Markdown.

The advantages would be:

  • no need to allow HTML and deal with all the filtering (that's good :-))
  • clear/familiar guide to formatting data
  • a lot of flexibility when you generate the HTML (in the end for display)

The disadvantages:

  • no HTML (blessing and curse ;-))
  • people might not be familiar the syntax
Till