Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a standard web form that accepts text dialog boxes for submission.
When I post <b>yo</b> into the text box, I get this:
%3Cb%3Eyo%3C%2Fb%3E
I'd like to be able to turn it back into the original, WITH the html tags. The only code snippets I found seem to rip the html off.
Can anyone point me to some code to convert the % characters back to the original codes?
thanks!

Replies are listed 'Best First'.
Re: html code cleanup via GET
by borisz (Canon) on Nov 02, 2004 at 11:29 UTC
    use URI::Escape; my $x = '%3Cb%3Eyo%3C%2Fb%3E'; print uri_unescape($x);
    Boris
      $string = "%3Cb%3Eyo%3C%2Fb%3E";\ $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
      Worked!
      should I be using CGI.pm rather then the code I presented?

        Yes. No sense reinventing the wheel, especially incorrectly.

        $string = "%3Cb%3Eyo%3C%2Fb%3E"; $string =~ s/+/ /sg; <---- You forgot this. $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/seg;
Re: html code cleanup via GET
by gellyfish (Monsignor) on Nov 02, 2004 at 11:23 UTC

    If you are using the CGI module then the URL-decoding of the query parameters will be done for you.

    /J\

Re: html code cleanup via GET
by Happy-the-monk (Canon) on Nov 02, 2004 at 11:25 UTC

    show us the code that did it!

    If you used CGI.pm as I'd recommend you, at least until you get everything working out the way you want to have it, then CGI.pm's param method would have converted your characters back to the original representation already.

    Cheers, Sören

      Here is the code I am using the grab my posted data. I got it from some book once upon a time, and have been using it since.
      if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { print "Content-type: text/html\n\n"; print "<P>Use Post or Get"; } foreach $pair (@pairs) { ($key, $value) = split (/=/, $pair); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~s/<!--(.|\n)*-->//g; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } $yip = $ENV{'REMOTE_ADDR'}; $link = $formdata{'link'};
      Link then prints out the bad link above.

        While I've seen worse form data parsers, this one doesn't handle multivalues and probably has security issues (although I don't remember why at the moment of this writing).

        You can avoid this and a lot of cut+paste by using CGI; the following code replaces the one in your post, gaining named features and not losing anything:

        use CGI; # use the CGI.pm module my $query = CGI->new; # create a CGI object called "$query +" # if you actually need all form data in a hash, # do this: my %formdata = $query->Vars; # get all form data in hash "%formda +ta". # does handle multivalues, although not too well; # see the CGI manual on "multivalue" for more on that topi +c. my $yip = $query->remote_host; # get the remote hostname or address # my $link = $formdata{link}; # one way of getting the link parame +ter. my $link = $query->param('link'); # another way of getting the link pa +rameter.

        Cheers, Sören

Re: html code cleanup via GET
by TedPride (Priest) on Nov 02, 2004 at 11:48 UTC
    s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;