Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode cyrillic characters for URL and then decode them?

I have a form on one page:

<form method="POST" accept-charset="UTF-8" action="index.cgi" name="TestForm">

One of the input fields "search_string" may be used to send Cyrillic characters and if that happens the URL string looks like this:

search_string=%41F%2F%424+%41F%41E%414%416%410%420%41A%410+%418%417+%421%412%418%41D

How do I decode this back to the original string on the page I post to?

like image 944
goe Avatar asked Mar 22 '12 08:03

goe


People also ask

Does UTF-8 have Cyrillic?

UTF-8. 128 characters are encoded using 1 byte (the ASCII characters). 1920 characters are encoded using 2 bytes (Roman, Greek, Cyrillic, Coptic, Armenian, Hebrew, Arabic characters).

How do you encode a character in a URL?

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

How do you add %20 to a URL?

Instead of encoding a space as “%20,” you can use the plus sign reserved character to represent a space. For example, the URL “http://www.example.com/products%20and%20services.html” can also be encoded as http://www.example.com/products+and+services.html.

How do you decode or encode a URL in Javascript?

Decoding a URLdecodeURI() function − The decodeURI() function is used to decode the URI, i.e., converting the special characters back to the original URI language. decodeURIComponent() function − This function decodes the complete URL back to its original form.


3 Answers

Correct solution, including spaces:

use open ':std', ':encoding(UTF-8)';
use Encode;

my $escaped = '%41F%2F%424+%41F%41E%414%416%410%420%41A%410+%418%417+%421%412%418%41D';
(my $unescaped = $escaped) =~ s/\+/ /g;
$unescaped =~ s/%([[:xdigit:]]+)/chr hex $1/eg;
print $unescaped;
# П/Ф ПОДЖАРКА ИЗ СВИН

Credit goes to Renaud Bompuis for recognising as the first that these are Unicode code-points prefixed with %.

I wish to add that the encoding scheme from the question is very unusual, I haven't seen it before. Normally one would expect the characters string П/Ф ПОДЖАРКА ИЗ СВИН to be encoded as %D0%9F%2F%D0%A4+%D0%9F%D0%9E%D0%94%D0%96%D0%90%D0%A0%D0%9A%D0%90+%D0%98%D0%97+%D0%A1%D0%92%D0%98%D0%9D, that is to say, first the characters are encoded into UTF-8, then the octets are percent-escaped. This scheme works with the answer from Dr.Kameleon.

like image 109
daxim Avatar answered Oct 22 '22 19:10

daxim


A solution that preserves the + and any other character in the original string:

my $s = '%41F%2F%424+%41F%41E%414%416%410%420%41A%410+%418%417+%421%412%418%41D';
$s =~ s/%([[:xdigit:]]+)/chr(hex($1))/eg;
print $s;

Result:

П/Ф+ПОДЖАРКА+ИЗ+СВИН
like image 30
Renaud Bompuis Avatar answered Oct 22 '22 19:10

Renaud Bompuis


Try that in your script (index.cgi) :

use Encode;

Then...

$search_string = decode_utf8( $search_string );

Another idea (if you want to create a UTF8-friendly hash of your CGI input) :

require Encode;
require CGI;
my $query = CGI ->new;
my $form_input = {};  
foreach my $name ( $query ->param ) {
  my @val = $query ->param( $name );
  foreach ( @val ) {
    $_ = Encode::decode_utf8( $_ );
  }
  $name = Encode::decode_utf8( $name );
  if ( scalar @val == 1 ) {   
    $form_input ->{$name} = $val[0];
  } else {                      
    $form_input ->{$name} = \@val;  # save value as an array ref
  }
}

Taken from : http://ahinea.com/en/tech/perl-unicode-struggle.html

like image 1
Dr.Kameleon Avatar answered Oct 22 '22 20:10

Dr.Kameleon