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

Dear all, www.perlmonks.org/index.pl?node=Seekers%20of%20Perl%20Wisdom from the above url string, is there anyway to recognize the %20 and change it back to a space. Or i need to write a loop command to split each character and make a check on it and then join it back again. Thank you. I'm very appreciate the help i can get.
  • Comment on Having problem with url space conversion

Replies are listed 'Best First'.
Re: Having problem with url space conversion
by n3dst4 (Scribe) on Jan 28, 2002 at 14:30 UTC
    As a very generic answer,

    $url =~ s/%20/ /g;

    On the other hand, try URI::Escape:

    $normal = uri_unescape($escaped);

    It unescapes anything URI-escaped. You can also reliably escape strings with uri_escape().
Re: Having problem with url space conversion
by strat (Canon) on Jan 28, 2002 at 17:05 UTC
    Or, in a more common way:
    $string =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    But I prefer using uri_escape or param from CGI...

    Best regards,
    perl -e "$_=*F=>y~\*martinF~stronat~=>s~<A HREF="/index.pl?node=%5E%5Cw&lastnode_id=1072">^\w</A>~~g=>chop,print"

Re: Having problem with url space conversion
by hopes (Friar) on Jan 28, 2002 at 17:31 UTC
    Just one more...
    (I also prefer URI::Escape)
    sub escape { my $toencode = shift; return undef unless defined($toencode); $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; return $toencode; }


    Hopes
    $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print
Re: Having problem with url space conversion
by trs80 (Priest) on Jan 28, 2002 at 19:58 UTC
    Straight From the URI::Escape docs:

    uri_unescape($string,...)

    Returns a string with all %XX sequences replaced with the actual byte (octet).

    This does the same as:
    $string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;

    but does not modify the string in-place as this RE would. Using the uri_unescape() function instead of the RE might make the code look cleaner and is a few characters less to type.

    In a simple benchmark test I made I got something like 40% slowdown by calling the function (instead of the inline RE above) if a few chars where unescaped and something like 700% slowdown if none where. If you are going to unescape a lot of times it might be a good idea to inline the RE.

    If the uri_unescape() function is passed multiple strings, then each one is unescaped returned.

    ------------------------------------
    based on Version 3.19 of URI::Escape
Re: Having problem with url space conversion
by Ido (Hermit) on Jan 28, 2002 at 18:42 UTC
    Why not using pack 'H'? Something like:
    s/%([[:xdigit:]]{2})/pack 'H',$1/eg;
Re: Having problem with url space conversion
by Aristotle (Chancellor) on Jan 28, 2002 at 23:04 UTC
    One question I'd like to ask is what you want this for? In case you're trying to write a CGI script, then use CGI; rather than rolling your own solution.

    Makeshifts last the longest.

      Becoz i need to get back the parameter past from the uri to make a search command base on the keyword. eg. www.perl.com/name=calvin%20boy. Anyway thank for all the help, i'm very appreciate all the help from u all. it really solve my problem.
Re: Having problem with url space conversion
by belg4mit (Prior) on Jan 29, 2002 at 04:39 UTC
    Okay, I guess I'll make the required mention of using taint. And why do I bring taint up here? Well all of these implementations are susceptible to poison null. Even if you're doing some data-validation without taint chances are you'd miss that.

    Also note that + is sometimes used to encode spaces as well.

    --
    perl -pe "s/\b;([st])/'\1/mg"