Welcome to WebmasterWorld Guest from

Forum Moderators: incrediBILL

Message Too Old, No Replies

keeping HTML source in a form

keeping HTML source in a table

2:57 pm on Jun 4, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 30, 2002
votes: 0

I have HTML source code in a DB. I built a small page to edit fields in the DB.

When I display the contents from the database into a textarea of a form I see the encoding, not the HTML source.

for example, I see < instead of &lt;

How do I call the source code into the <textarea> and have it display it as &lt; v. <



3:25 pm on June 4, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

I do this by programming.

Substitute when it comes out

$texfield =~ s/</\&lt\;/ig;
$texfield =~ s/>/\&gt\;/ig;

and convert it back when it goes in:

$texfield =~ s/\&lt\;/</ig;
$texfield =~ s/\&gt\;/>/ig;

So that when it's called from a "public" page, it displays as expected, yet doesn't screw up the form when you edit it.

3:26 pm on June 4, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0

I think you'll need to essentially 'double-encode' your html entities before outputting to the textarea. Essentially converting all '&' into '&amp;'. However, there is a bit of a cross-browser issue if you have html entities within html comments - a problem I had a short while back... see this thread for more info and my post at the end for an explanation of your problem...

Regular expression - How to replace all &'s within an HTML comment [webmasterworld.com]

(an HTML source code editor...) The HTML content is loaded into a <textarea> to be edited. HTML entities, however, get converted by the browser into the actual characters for display so the HTML entity is lost. So &lt; is converted into &amp;lt; so that it actually displays in the textarea as &lt;. However, I've found that in IE and FF HTML entities that appear within HTML comments do not get converted into the actual characters, they stay as HTML entities (hence this thread), except in Opera they do (hhmmmm)!

(I think rocknbil types quicker than me ;)

Just to note... If you are allowing your users to enter HTML source to be saved as is (including entities), you probably don't need to 'convert this back' before saving? Or are there special cases?
eg. &lt; gets converted to &amp;lt; before writing it out to your page server-side (between <textarea> tags). This displays correctly as &lt; on the client side and this should get returned as-is to get plugged back into your database.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members