[thelist] URL decode

Steve Webster steve at netbreed.co.uk
Sat Oct 5 11:05:01 CDT 2002


Hi Kevin,

> I need to progrmatically decode the following UTF-8 data via CGI:
>
> q=%D7%91%D7%A8%D7%99%D7%AA
>
> Can this be done?

Yes it can be done, but the solution depends on which language you're
using for the CGI. I'll take a guess at it being Perl.

The easiest way to do this would be to use the CGI.pm module as it
handles all the horrible decoding work for you...

#!/usr/bin/perl
use CGI;

// Create a new instance of CGI
$query = new CGI;

// Fetch variable
$q = $query->param('q');

If you don't have the luxury of CGI.pm, then you can alsways do it
manually. After fetching the data out of the input stream (or from the
query string for GET data), use the following line to decode those
characters...

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/seg;

I hope this helps!

Regards,

Steve





More information about the thelist mailing list