Archive for the ‘html’ tag
CGI escape, unescape, escapeHTML, unescapeHTML
I just can’t seem to remember which to use when so here are the 4 great helpers when working with URLs and HTML:
When constructing URLs, you use escape and unescape:
url = "http://site.com/?address=" + CGI.escape("123 Main St, City, ST, 99999") # url: http://example.com/?address=123+Main+St%2C+City%2C+ST%2C+99999
unescape does the reverse.
When rendering text inside HTML page, you use escapeHTML and unescapeHTML:
raw_html = CGI::escapeHTML("<div>An example HTML to be displayed raw on an HTML page</div>") #raw_html: <div>An example HTML to be displayed raw on an HTML page</div>
unescapeHTML is for converting an html-escaped string back to proper HTML form.
Simple, but you often forget!