Page 1 of 1

HTML Encode / Decode

Posted: Fri May 12, 2006 4:17 pm
by peterro
I am inserting data from a textarea into a text field in a database. I need to allow for two things.
1. I need to allow special characters which is easy using addslashes.
2. I need to preserve html formatting. I am using overlib to display the data. If I enter:

The Dog Is
Brown
Then I would like it to display in overlib the same way. Is there a html encode / decode function that I could use?

Thanks!

Re: HTML Encode / Decode

Posted: Sat May 13, 2006 4:19 am
by aerodromoi
peterro wrote:If I enter:

The Dog Is
Brown
Then I would like it to display in overlib the same way. Is there a html encode / decode function that I could use?

Thanks!
From what I gather you don't need any "html encode / decode function" - you just have to convert linebreaks "\r\n" to <br />.
eg. with preg_replace

aerodromoi

Posted: Sat May 13, 2006 8:59 am
by timvw

Posted: Sun May 14, 2006 12:09 pm
by alex-weej

Code: Select all

printf('<textarea>%s</textarea>', htmlspecialchars($string, ENT_NOQUOTES, 'UTF-8'));
... 100% bulletproof. ENT_NOQUOTES (vs. ENT_COMPAT or ENT_QUOTES) disables quote encoding, as it's not needed outside of attribute markup. Setting the encoding type really isn't so important with htmlspecialchars(), but it is HUGELY important with htmlentities(), because otherwise, multi-byte characters (like £) will be interpreted as single-byte characters and converted to HTML named entities.

PS: don't use nl2br, you will introduce rogue <br> tags into your data.