# Gsub with Hash$B$r;H$C$?(BCGI::unescapeHTML

**URL:** <https://www.ruby-forum.com/t/gsub-with-hash-b-r-h-c-bcgi-unescapehtml/134965>\
**Category:** Development\
**Created:** [April 16, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/gsub-with-hash-b-r-h-c-bcgi-unescapehtml/134965 "2008-04-16T17:48:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/77aa72/32.png) [@Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/u/Kazuhiro_NISHIYAMA)\
**Post date:** [April 16, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/gsub-with-hash-b-r-h-c-bcgi-unescapehtml/134965/1 "2008-04-16T17:48:38Z")

</div>

e$B@\>;3OB9-$G$9!#e(B

e$B:#$N$H$3$me(B[ruby-core:15637]e$B$,F~$C$F$$$J$$$N$G!"e(Bencodee$B$Ne(Binvalide$B$N$H$3$m$,e(B  
e$B$@$a$G$9$,!"e(BCGI::unescapeHTMLe$B$Oe(Bgsub with  
Hashe$B$r;H$&$H0J2\<$N$h$&$Ke(B  
e$B$G$-$k$N$G$O$J$$$G$7$g$&$+!#e(B

def CGI::unescapeHTML(string)  
enc = string.encoding  
h = Hash.new do |hash, key|  
if $2  
[Integer($2)].pack(“U”).force\_encoding(Encoding::UTF\_8).encode(enc,  
invalid: :dec\_charref)  
elsif $3  
[$3.hex].pack(“U”).force\_encoding(Encoding::UTF\_8).encode(enc,  
invalid: :hex\_charref)  
else  
key  
end  
end  
h[’&’] = ‘&’  
h[’"’] = ‘"’  
h[’\>’] = ‘\<’  
h[’\<’] = ‘\>’  
string.gsub(/&(amp|quot|gt|lt|#0\*([0-9]+)|#x([0-9A-Fa-f]+));/, h)  
end

pack(“U”)e$B$,e(BASCII-8BITe$B$NJ8;zNs$rJV$7$F$/$k$N$Ge(B  
force\_encoding(Encoding::UTF\_8)e$B$,I,MW$H$+!"e(BHashe$B$Ne(Bdefault\_proce$B$NCf$Ge(B  
ence$B$,I,MW$J$N$G!"Kh2se(BHashe$B$r@8@.$7$J$$$H9T$1$J$$$H$+$,5$$K$J$j$^$9$,!#e(B

---

<div class="post-metadata">

**Author:** ![Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/77aa72/32.png) [@Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/u/Kazuhiro_NISHIYAMA)\
**Post date:** [April 16, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/gsub-with-hash-b-r-h-c-bcgi-unescapehtml/134965/2 "2008-04-16T18:48:00Z")

</div>

e$B@.@%$G$9!#e(B

Kazuhiro NISHIYAMA wrote:

> e$B:#$N$H$3$me(B[ruby-core:15637]e$B$,F~$C$F$$$J$$$N$G!"e(Bencodee$B$Ne(Binvalide$B$N$H$3$m$,e(B  
> e$B$@$a$G$9$,!"e(B

e$B$3$l$I$&$J$N$G$7$g$&$+$M!#e(B  
e$B$9$G$K\<BAu:Q$_$J$N$G$7$?$i$H$j$"$($:%3%_%C%H$7$FD:$1$k$H$"$j$,$?$$$N$G$9$,!#e(B

# e$B$"$He(B ISO-2022-JP e$B$H$+e(B CP932 e$B$H$+e(B

> ```
> else
> 
> ```
> 
> pack(“U”)e$B$,e(BASCII-8BITe$B$NJ8;zNs$rJV$7$F$/$k$N$Ge(B  
> pack(“U”) e$B$de(B pack(“U\*”) e$B$@$He(B UTF-8  
> e$B$GJV$7$F$b$$$$5$$,$7$F$/$k$N$G$9$,!"e(B  
> [0x100, 0x80].pack(“UC”) e$B$H$+$r9M$($k$HHyL/$K$J$C$F$-$^$9!#e(B

> force\_encoding(Encoding::UTF\_8)e$B$,I,MW$H$+e(B

e$B$3$l$Oe(B String#encode(to, from, opt) e$B$r;H$($PITMW$G$9!#e(B

> Hashe$B$Ne(Bdefault\_proce$B$NCf$Ge(B  
> ence$B$,I,MW$J$N$G!"Kh2se(BHashe$B$r@8@.$7$J$$$H9T$1$J$$$H$+$,5$$K$J$j$^$9$,!#e(B

UNESCAPE\_HTML\_TABLE = Hash.new  
e$B$H$G$b$7$F$\*$$$F!“e(B  
UNESCAPE\_HTML\_TABLE[enc] = h  
e$B$”$?$j$G%-%c%C%7%e$9$l$PKh2s@8@.$7$J$/$F:Q$\_$^$9$M!#e(B
