sub uri_unescape { my $to_decode = shift; return if ! defined $to_decode; $to_decode =~ s/\+/ /g; # A "good enough" check to see if the browser encoded high bit # characters as UTF-8 by looking for something that resembles a # URL-encoded utf8 octet series # # utf8 encoding starts with 11xxxxxx in the first byte and # 10xxxxxx in the subsequent bytes # http://en.wikipedia.org/wiki/UTF-8#Description my $is_byte_encoded_utf8 = $to_decode =~ /%[c-fC-F][[:xdigit:]]%[89abAB][[:xdigit:]]/; $to_decode =~ s/%([[:xdigit:]]{2})/chr hex "0x$1"/eg; # Decode the "UTF-8" if it looked like it was such. If I enter # this block, the string may now be UTF-8 encoded as a perl # string. if ($is_byte_encoded_utf8) { $to_decode = Encode::decode( 'utf8', $to_decode ); } # Promote %uXXXX escapes up to characters. This is after the # Encode call so I don't inadvertently cause a promotion to utf8 # and then ask Encode to do another promotion. $to_decode =~ s/%u([[:xdigit:]]{4})/chr hex "0x$1"/eg; return $to_decode; }