Главная страница


ru.perl

 
 - RU.PERL ----------------------------------------------------------------------
 From : Serge Shikov                         2:5020/400     25 Jul 2000  12:33:16
 To : All
 Subject : Re: URL-encoding
 -------------------------------------------------------------------------------- 
 
 Artem Chuprina wrote:
 
 > 
 > >> >Hе подскажет кто regexp для кодирования/декодирования
 > >> > URL-я в form-url-encoded.
 > >> >Чтобы всякие русские буквы и не-урлевые символы заменялись
 > >> >на %XX, где XX - 16-ричный код символа.
 > >>
 > >> Соответствующие функции escape(), unescape() есть в CGI.pm. Только
 > >> perldoc CGI о них помалкивает, и включать их придется в явном виде:
 > srr> А не лучше взять модуль, который специально для этого написан -
 > srr> URI::URL?
 > 
 > Hет. Функции одни и те же. URI::URL придуман для читателей - LWP и парсеров
 > HTML. А чукча - писатель.
 
 А какая разница, если функции якобы одни и теже? Впрочем, они на самом
 деле очень похожие, но не одинаковые.
 
 Эта - из URI::Escape:
 sub uri_unescape
 {
     # Note from RFC1630:  "Sequences which start with a percent sign
     # but are not followed by two hexadecimal characters are reserved
     # for future extension"
     my $str = shift;
     if (@_ && wantarray) {
   # not executed for the common case of a single argument
   my @str = @_;  # need to copy
   return map { s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg } $str, @str;
     }
     $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
     $str;
 }
 
 А эта - из CGI:
 # unescape URL-encoded data
 sub unescape {
     shift() if ref($_[0]);
     my $todecode = shift;
     return undef unless defined($todecode);
     $todecode =~ tr/+/ /;       # pluses become spaces
     $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
     return $todecode;
 }
 
 Снова URI::Escape:
 # Build a char->hex map
 for (0..255) {
     $escapes{chr($_)} = sprintf("%%%02X", $_);
 }
 
 my %subst;  # compiled patternes
 
 sub uri_escape
 {
     my($text, $patn) = @_;
     return undef unless defined $text;
     if (defined $patn){
   unless (exists  $subst{$patn}) {
       # Because we can't compile the regex we fake it with a cached sub
       $subst{$patn} =
         eval "sub {\$_[0] =~ s/([$patn])/\$escapes{\$1}/g; }";
       Carp::croak("uri_escape: $@") if $@;
   }
   &{$subst{$patn}}($text);
     } else {
   # Default unsafe characters. (RFC 2396 ^uric)
   $text =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;
     }
     $text;
 }
 
 CGI.pm:
 # URL-encode data
 sub escape {
     shift() if ref($_[0]) || $_[0] eq $DefaultClass;
     my $toencode = shift;
     return undef unless defined($toencode);
     $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
     return $toencode;
 }
 --- ifmail v.2.15dev5
  * Origin: home (2:5020/400)
 
 

Вернуться к списку тем, сортированных по: возрастание даты  уменьшение даты  тема  автор 

 Тема:    Автор:    Дата:  
 Re: URL-encoding   Serge Shikov   25 Jul 2000 12:33:16 
Архивное /ru.perl/2825dc6eaa70.html, оценка 1 из 5, голосов 10
Яндекс.Метрика
Valid HTML 4.01 Transitional