Re: Formatting Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, October 2, 2006 12:08 pm, Toby Osbourn wrote:
> Sorry to plague you with a question that should have a simple answer,
> but I
> can't find said answer anywhere (probably looking for the wrong things
> in
> the wrong places!)
>
> Basically I want to allow a user to input a string via a form into a
> database, I have the code to do that and it works almost 100% - I have
> made
> it so that any html tags are stripping off the string before saving
> it, but
> I do want to allow some basic formatting - namely when a user takes a
> new
> line in the textbox, I want the new line to carry over to the
> database.
>
> At the moment the user could type in something like this...
>
> wibble
> wobble
>
> But after it has been submitted and then retrieved from the database
> it will
> return wibblewobble.

You may THINK you are seeing "wibble wobble" in your browser, but
that's because a browser smushes all whitespace into just one
whitespace -- that's why you need   all over the place in the Bad
Old Days... :-)

Anyway, you could be managing to strip out the newlines, I suppose, in
which case you have to figure out where you are doing that.

Echo out the data as it travels into/through/out-of your system.

echo "<pre>"; htmlentities($whatever); "</pre>";

For removing the HTML, use http://php.net/striptags

For ancient browsers that give non-standard newlines do like this with
the input:
$text = str_replace("\r\n", "\n", $text); //non-standard Windows browsers
$text = str_replace("\r", "\n", $text); //non-standard Mac browsers

Then, on *OUTPUT* to a browser, only on output to a browser, you can
use http://php.net/nl2br to format the newlines for HTML.

The reason for doing this only on output is this:
Some day, even if you don't think you will, you *might* need to output
this to someting other than a browser.  RSS, CSV dump, or some new
fancy format we haven't even invented yet.

Don't "pollute" your raw data (the newlines) with a very
media-specific formatting code ("<br />") -- Keep your raw data pure
and clean, and format for the destination when you send it there, not
when you store it.

There *might* be some egregious examples of over-loaded high-volume
servers where adding the "<br />" at run-time is "too much work" -- At
that point, it's probably still not the "Right Answer" to pollute the
raw data.  It might be expensive, but adding a cache of the output
data, or even a second field for data_as_html to the database should
be considered.  Anything other than polluting your raw data.

This may seem high-falutin' purism, but it will make your life sooooo
much more pleasant some day down the road.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux