Textarea to paragraphs
When I have to render textarea content to the front end I usually pass it thru a function that converts newlines to <br/>
tags and double newlines signal paragraph tags so blocks of text get surrounded by <p>
and </p>
tags.
To save time I开发者_如何学JAVA usually use a ready made PHP function from the wordpress codebase. You can get the link from the man himself: http://ma.tt/scripts/autop/
If you check it out you'll see it does some heavy lifting with about 20 regular expressions.
I know I could use a wysiwyg editor (like TinyMCE or CKEditor) that can format the data on the client and then send it to the server (most of them add <p>..</p>
tags by default) but I want to know the experience of others in handling raw textarea input and then displaying it on the front end.
EDIT: Wow, was expecting more responses to this. Either most people use wysiwyg editors and/or direct output to browser and/or use basic functions like nl2br. I'll give this question another day.
If you check it out you'll see it does some heavy lifting with about 20 regular expressions.
I think you shouldn't be afraid of 20 regular expressions. They aren't demons of speed, but they aren't so slow in fact. Most of sites converts content from some kind of BBCode/Textile/Markdown/others format to HTML just before displaying... and often they do this 20 times for a single page.
精彩评论