Forum Moderators: coopster
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]
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...
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...
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.
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. :)
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