HTML Encode / Decode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peterro
Forum Newbie
Posts: 3
Joined: Fri May 12, 2006 11:30 am

HTML Encode / Decode

Post 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!
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: HTML Encode / Decode

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post 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.
Post Reply