At 14:36 +0200 2001.08.21, Jonas Liljegren wrote:
>Stuart Johnston <[EMAIL PROTECTED]> writes:
>
>> Does anyone have a simple filter for URL encoding that I can use?
>
>Here is a simple filter, in context:
>
>     $th = Template->new(
>         FILTERS =>
>         {
>             'uri' => sub { CGI::escape($_[0]) },
>         }
>        );

Except that this will properly encode URI *parameters*, not a URI itself.

   http://foo/bar&baz -> http%3A%2F%2Ffoo%2Fbar%26baz

That might be what is wanted, dunno.  I have two filters I use, fixurl()
and fixparam().  My fixparam is:

sub fixparam {
        my($param) = @_;
        $param =~ s/([^$URI::unreserved])/$URI::Escape::escapes{$1}/oge;
        return $param;
}

It is slightly better than using CGI::escape, since it only escapes those
elements which *must* be escaped, according to the RFC.  CGI::escape encodes
[^a-zA-Z0-9_.-] while $URI::unreserved does [^a-zA-Z0-9_.-!~*'()] (with
appropriate quoting of metachars).

-- 
Chris Nandor                      [EMAIL PROTECTED]    http://pudge.net/
Open Source Development Network    [EMAIL PROTECTED]     http://osdn.com/


Reply via email to