Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

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

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

require 'cgi'

# escape
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
html = %(<a href="#{CGI.escapeHTML(url)}">example</a>)
# html: <a href="http://example.com/?ruby%3F=yes&amp;var=T">example</a>

# unescape
name_encoded = html.match(/http:([^"]+)/)[0]
# name_encoded: http://example.com/?ruby%3F=yes&amp;var=T
href = CGI.unescapeHTML(name_encoded)
# href: http://example.com/?ruby%3F=yes&var=T
query = href.match(/\?(.*)$/)[1]
# 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"]

Nice post slug

This code replaces accents to normal chars(e.g "á" => "a"), everything that isn´t in "a-zA-Z0-9" to "", multiples spaces to one space, and one space to "-".

This is useful to make URLs from titles, like Netscape.com does...

"A new report recommends only work 4 -6 hrs a day" => "A-new-report-recommends-only-work-4-6-hrs-a-day"

"Video: \"Popular Mechanics\" editor debunks 9/11 myths" => "Video-Popular-Mechanics-editor-debunks-911-myths"

etc..
The code is 95% based on http://textsnippets.com/posts/show/451

def self.nice_slug(str)
		
		accents = { 
		  ['á','à','â','ä','ã'] => 'a',
		  ['Ã','Ä','Â','À','�'] => 'A',
		  ['é','è','ê','ë'] => 'e',
		  ['Ë','É','È','Ê'] => 'E',
		  ['í','ì','î','ï'] => 'i',
		  ['�','Î','Ì','�'] => 'I',
		  ['ó','ò','ô','ö','õ'] => 'o',
		  ['Õ','Ö','Ô','Ò','Ó'] => 'O',
		  ['ú','ù','û','ü'] => 'u',
		  ['Ú','Û','Ù','Ü'] => 'U',
		  ['ç'] => 'c', ['Ç'] => 'C',
		  ['ñ'] => 'n', ['Ñ'] => 'N'
		  }
		accents.each do |ac,rep|
		  ac.each do |s|
			str = str.gsub(s, rep)
		  end
		end
		str = str.gsub(/[^a-zA-Z0-9 ]/,"")
		
		str = str.gsub(/[ ]+/," ")
		

		str = str.gsub(/ /,"-")
		
		#str = str.downcase

	end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS