http://www.perlmonks.org?node_id=89754


in reply to Regex to cange HTML %?? to char(0x??);

Well, here's a solution, but there's a module that does this--I just can't think of it now. Give CPAN a good look, and someone will probably respond with it. Here's the solution:
$str =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr(hex($1))/eg;

Replies are listed 'Best First'.
Re: Re: Regex to cange HTML %?? to char(0x??);
by TGI (Parson) on Jun 19, 2001 at 23:56 UTC

    How about these?

    # The above shrunk a bit. $str =~ s/%([0-9A-F][0-9A-F])/chr(hex($1))/ieg; # - or more cryptic $str =~ s/%([0-9A-F][0-9A-F])/sprintf("%1c",hex($1))/ieg;

    Can anyone think of something that doesn't use hex?


    TGI says moo

    Update

    Bill's post and Abigail's clever method taken together let us chop off another character or so:

    s/%([0-9A-F]{2})/"chr 0x$1"/igee;

    TGI - Syncretic Cretin

      Can anyone think of something that doesn't use hex?
      s/%([0-9A-F][0-9A-F])/"chr 0x$1"/ieeg;

      -- Abigail