Moxley Stratton

Code Blog

Ruby: Escape, Unescape, Encode, Decode, HTML, XML, URI, URL

This example shows you how to escape and un-escape a value to be included in a URI and within HTML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'cgi'

# Escape data for URL query parameters
name = "ruby?"
value = "yes"
url = "http://example.com/?" + CGI.escape(name) + '=' + CGI.escape(value) + "&var=T"
# url: http://example.com/?ruby%3F=yes&var=T

# Escape data for HTML
url = "http://example.com/?ruby%3F=yes&var=T"
html = %(<a href="#{CGI.escapeHTML(url)}">example</a>)
# html: <a href="http://example.com/?ruby%3F=yes&amp;var=T">example</a>

# Unescape HTML-escaped data
url = CGI.unescapeHTML("http://example.com/?ruby%3F=yes&amp;var=T")
# url: http://example.com/?ruby%3F=yes&var=T

# Parse query params:
query = "ruby%3F=yes&var=T"
pairs = query.split('&')    # pairs: ["ruby%3F=yes", "var=T"]
name, value = pairs[0].split('=').map{|v| CGI.unescape(v)}
# name, value: ["ruby?", "yes"]