URLEncode

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 robinkin 的 blog

sub url_encode {
my $text = shift;
$text =~ s/([^a-z0-9_.!~*'() -])/sprintf "%%%02x", ord($1)/egi;
$text =~ tr/ /+/;
return $text;
}


sub url_decode {
my $text = shift;
$text =~ tr/\+/ /;
$text =~ s/%([a-f0-9][a-f0-9])/chr( hex( $1 ) )/egi;
return $text;
}

function urlencode(plaintext )
{
 // the javascript escape and unescape functions do not correspond
 // with what browsers actually do...
 var safechars = "0123456789" +     // numeric
     "abcdefghijklmnopqrstuvwxyz" + // alphabetic
     "abcdefghijklmnopqrstuvwxyz" +
     "-_.!~*'()";     // rfc2396 mark characters
 var hex = "0123456789abcdef";

 var encoded = "";
 for (var i = 0; i < plaintext.length; i++ ) {
  var ch = plaintext.charat(i);
     if (ch == " ") {
      encoded += "+";    // x-www-urlencoded, rather than %20
  } else if (safechars.indexof(ch) != -1) {
      encoded += ch;
  } else {
      var charcode = ch.charcodeat(0);
   if (charcode > 255) {
       alert( "unicode character '"
                        + ch
                        + "' cannot be encoded using standard url encoding.\n" +
              "(url encoding only supports 8-bit characters.)\n" +
        "a space (+) will be substituted." );
    encoded += "+";
   } else {
    encoded += "%";
    encoded += hex.charat((charcode >> 4) & 0xf);
    encoded += hex.charat(charcode & 0xf);
   }
  }
 } // for

 return encoded;
};

本文关键:URLEncode
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top