HI all. I'm having a serious brain glitch here, bu...
# cfml-general
l
HI all. I'm having a serious brain glitch here, but does anyone have a fairly quick and dirty way of converting punctuation, such as periods, commas, exclamation points, apostrophes etc to their HTML entity equivalents? Many thanks.
r
If it is a limited set of characters like you’ve listed and it is just plain text that will get wrapped in HTML (given that you’re changing them to HTML entities), any sort of replace() or replaceNoCase() call would likely do it. but if you’re dealing with those in text that also contains HTML markup/CSS/JS/etc., that’s a whole different problem as a global replace risks changing stuff inside the markup/etc. in addition to any in the text itself.
d
You have text that you want to show on an HTML page? The punctuation you listed should be fine as-is in HTML. Leaving aside unicode, extended characters, and non-English ones, the only chars that need to be encoded are ones that are significant in HTML markup, i.e., &, <, >, plus single and double quotes inside attribute values. To back that up, see here.
What you probably want to do is use encodeForHTML(someText). That's MANDATORY if it's user-entered text. That covers a lot of extended stuff and guards at least somewhat again XSS hacks.
4
l
Thanks all for your input. As I mentioned it was a bit of a brain freeze on my part.