Jump to content
Larry Ullman's Book Forums

Avoid Conversion Of Html Entities In A Text Area


Recommended Posts

I'm pulling text from a database and putting it into an HTML text area as a rudimentary CMS back end. The text being pulled from the database contains a fair amount of HTML, for which HTML entities are being used for some of the greater/less than symbols. The entities are properly preserved in the database, but when I output the text to the text area, all the entities are changed to their normal symbols (< changes to < and > changes to >).

 

Is there any way to stop this annoying behavior?

 

It's probably worth mentioning that I'm using Ajax to get the text from the database and return it from a PHP script to the JavaScript. Perhaps this is where the issue is, but I really don't know. Any advice would be appreciated.

 

Most related info on the Net suggests just using regexes to convert back to the entities, but that is unacceptable, as I have greater/less than symbols that I want to be viewable as entities, and others that I want to just be standard greater/less than symbols.

 

Thanks.

Link to comment
Share on other sites

If htmlentities() was used to convert the text entered, pass the text through html_entity_decode() before printing. If htmlspecialchars() was used, then use htmlspecialchars_decode().You may have to break up the text to preserve the ones you want to retain as entities.

 

Edit: Oh wait, I just re-read your post and you're wanting to preserve some of the <, not convert them. I think you are still going to need to break up the text somehow. Then you can convert some entities, but not others. Might be tough to figure out where to break up the text, though. Is there any way you could tag the entities to preserve prior to inserting it into the database? Maybe use ~lt; and ~gt; instead of < and >? Then you could do a string replacement before printing.

 

htmlspecialchars_decode() should be faster than html_entity_decode(), since it works on a smaller subset of entities.

Link to comment
Share on other sites

Yeah, I was thinking about something like converting < and > to &lt; and &gt; before displaying them on the screen.

 

After playing around with it more yesterday, < and > are definitely preserved in the database properly, and if I just output to the screen, everything is okay. However, if I output to a text area, then the entity conversation takes place, which I don't want.

 

Anyway, thanks.

Link to comment
Share on other sites

 Share

×
×
  • Create New...