Forum Moderators: coopster

Message Too Old, No Replies

Reversing Carriage Returns into textareas

         

wruk999

2:11 pm on Aug 13, 2003 (gmt 0)

10+ Year Member



Hi,

In this [webmasterworld.com] thread, grahamstewart posted a set of four expression to take the input of a text field, and preserve the carriage returns etc.

Here is what ws posted:

// Use \n for newline on all systems
$html = preg_replace("/(\r\n¦\n¦\r)/", "\n", $html);

// Only allow two newlines in a row.
$html = preg_replace("/\n\n+/", "\n\n", $html);

// Put <p>..</p> around paragraphs
$html = preg_replace('/\n?(.+?)(\n\n¦\z)/s', "<p>$1</p>", $html);

// Convert newlines not preceded by </p> to a <br /> tag
$html = preg_replace('¦(?<!</p>)\s*\n¦', "<br />", $html);

Now, is it possible to get the info back from the database (which contains the html markup) and put it into a text box with the carriage returns in instead of the html markup. A kind of reverse of above.

Cheers,
wruk999

[edited by: wruk999 at 2:16 pm (utc) on Aug. 13, 2003]

wruk999

2:14 pm on Aug 13, 2003 (gmt 0)

10+ Year Member



<thought>Is it better to enter the standard text into the database, and then use the code snippet above to output the text on the page? Only thing is, that will be a load of resources used everytime page is loaded </thought>

dmorison

2:17 pm on Aug 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wruk999,

If you're intent on coming out of a database and back into an edit box (such as when you edit a post here on WebmasterWorld); then you shouldn't really be storing HTML in your database.

A much neater solution is to store the raw text in your database - warts'n'all.

When you want to display the content within a normal page, process the string as above to create <BR>'s from new-lines etc.

When you want the user to edit content, you just copy the string directly into a <textarea> element via html_entities().

Sorry to have not answered your question directly tho...

dmorison

2:19 pm on Aug 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



adds: Yes, there will be a minor overhead when rendering a page but in my opinion it is insignificant compared with db queries etc.

Especially if the text is only short. Not a problem.

Also consider how else you might want to use the data in the future. Perhaps you want to publish an XML/RSS version of the page - you certainly don't want your text to be HTML'ized then...

wruk999

2:31 pm on Aug 13, 2003 (gmt 0)

10+ Year Member



Hi dmorison,

That sounds a better idea. Thanks for your advice. :)

Cheers,
wruk999

vincevincevince

4:57 pm on Aug 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about storing both? Then for outputting the page, you use the HTML version, and for editing/RSS you use the raw version... remembering to overwrite the HTML fresh every time the raw is edited?

dmorison

6:14 pm on Aug 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



vince3 wrote:

How about storing both?

That is exactly what I would do were the time taken to render a page ever likely to become an issue.

Without seeing the source to BestBBS i'd wager that Brett is doing something along those lines within this system.

For a large scale BBS system such as this they may well be in different databases / tables. For page viewing, a pre-formatted output is pulled from a table dedicated as such, whereas user editing takes place on a different database / table.

Transactions ensure that consistency is maintained between tables etc.

jonknee

8:51 pm on Aug 14, 2003 (gmt 0)

10+ Year Member



Maybe I am missing something here... But what is wrong with nl2br()? As others haves said, you should store the raw in the DB. When you want to output to www, just run it through nl2br. When you want to output to email or what have you, just keep the \n's.

You do NOT want to store both. That will just slow things down when you are running a SELECT, and take up more space. You have to remember that the slowest part of a database powered script is the database. If you aren't going to have a lot of rows, this might not be a problem though. :)

wruk999

12:54 pm on Aug 16, 2003 (gmt 0)

10+ Year Member



Hi jonknee,

nl2br() only inserts the br tags, but not the p tags. The preg_replaces grahamstewart had posted in the linked thread, will convert line breaks into <br> and paragraphs into <p> ... </p> :)

vincevincevince: that sounds like the best idea for my situation. Thanks for the suggestion.

Thanks all.

wruk999