unescaping html

V

Vivek

Hi,
Is there a method or any library which allows me to unescape encoded
html. For example If I have hi%5Bthere%5D
I should get hi[there]

vivek
 
M

Michael Morin

Vivek said:
Hi,
Is there a method or any library which allows me to unescape encoded
html. For example If I have hi%5Bthere%5D
I should get hi[there]

vivek

I believe you're looking for the CGI::unescape method. If for some
reason you don't have that, you can do a simple gsub like this, though
it can fail in many ways and only handles hex entities.

gsub(/%([[:xdigit:]]{2})/) {|c| $1.hex.chr }

--
Michael Morin
Guide to Ruby
http://ruby.about.com/
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company
 
E

Emmanuel Oga

Vivek said:
Hi,
Is there a method or any library which allows me to unescape encoded
html. For example If I have hi%5Bthere%5D
I should get hi[there]

vivek

encoded _HTML_ ? what you will need is hpricot (which is amazing :)

require 'rubygems'
require 'hpricot'

Hpricot("hi>there<", :xhtml_strict => true).to_plain_text # =>
"hi>there<"

But! from your example seems like you don't want to unescape html but
the url:

require 'cgi'

CGI.unescape "hi%5Bthere%5D"
=> "hi[there]"

Greets!
 
L

Lars Christensen

 Is there a method or any library which allows me to unescape encoded
html. For example If I have hi%5Bthere%5D
I should get hi[there]

I think you mean encoded URL, not encoded HTML. For URL encoding, you
can use URL.unescape, or CGI.unescape as Michael sugests.

HTML escaping is a bit more complex, but the CGI has some methods,
e.g. CGI.escapeHTML/CGI.unescapeHTML.
 
V

Vivek

Thanks for the replies , actually I want to unescape some form
parameters which contain [ and ] .
The CGI.unescape works..Is it a superset of what URL.unescape does?

 Is there a method or any library which allows me to unescape encoded
html. For example If I have hi%5Bthere%5D
I should get hi[there]

I think you mean encoded URL, not encoded HTML. For URL encoding, you
can use URL.unescape, or CGI.unescape as Michael sugests.

HTML escaping is a bit more complex, but the CGI has some methods,
e.g. CGI.escapeHTML/CGI.unescapeHTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top