2013-07-26 219 views
5

VBScriptで文字列をURLデコードする必要があります。文字列には、UTF-8ごとに複数のバイトとしてエンコードされたUnicode文字を含めることができます。したがって、たとえば "Paris%20%E2%86%92%20Z%C3%BCrich"は "Paris→Zürich"にデコードされます。VBScriptでURLエンコードされたUTF-8文字列をデコードする

Function URLDecode(str) 
    set list = CreateObject("System.Collections.ArrayList") 
    strLen = Len(str) 
    for i = 1 to strLen 
     sT = mid(str, i, 1) 
     if sT = "%" then 
      if i + 2 <= strLen then 
       list.Add cbyte("&H" & mid(str, i + 1, 2)) 
       i = i + 2 
      end if 
     else 
      list.Add asc(sT) 
     end if 
    next 
    depth = 0 
    for each by in list.ToArray() 
     if by and &h80 then 
      if (by and &h40) = 0 then 
       if depth = 0 then Err.Raise 5 
       val = val * 2^6 + (by and &h3f) 
       depth = depth - 1 
       if depth = 0 then 
        sR = sR & chrw(val) 
        val = 0 
       end if 
      elseif (by and &h20) = 0 then 
       if depth > 0 then Err.Raise 5 
       val = by and &h1f 
       depth = 1 
      elseif (by and &h10) = 0 then 
       if depth > 0 then Err.Raise 5 
       val = by and &h0f 
       depth = 2 
      else 
       Err.Raise 5 
      end if 
     else 
      if depth > 0 then Err.Raise 5 
      sR = sR & chrw(by) 
     end if 
    next 
    if depth > 0 then Err.Raise 5 
    URLDecode = sR 
End Function 

これはうまく動作しているようだが、それは私には誇張して複雑になります。

は、私のようなコードを使用しています、仕事をします。 HTML5とWeb標準の時代には、手作業のループや条件がなくてもこれを実現するための簡単な方法が必要です。助言がありますか?

答えて

15

3つの異なる環境で3つの方法を示したいとします。これらのメソッドはすべて、JScriptのencodeURIComponentdecodeURIComponentの関数を必要とします。 ASPにおいて

1、サーバー側のJavaScriptを使用して最も適切な解決策の一つである:

<script language="javascript" runat="server"> 
URL = { 
    encode : function(s){return encodeURIComponent(s).replace(/'/g,"%27").replace(/"/g,"%22")}, 
    decode : function(s){return decodeURIComponent(s.replace(/\+/g, " "))} 
} 
</script> 
<% 
Response.Write URL.decode("Paris%20%E2%86%92%20Z%C3%BCrich") 
Response.Write URL.encode("Paris → Zürich") 
%> 

2. 32ビットのみ(起因MSScriptControl.ScriptControlに32ビットのみの成分であります64ビットのサポートにより

Dim JSEngine 
Set JSEngine = CreateObject("MSScriptControl.ScriptControl") 
    JSEngine.Language = "JScript" 

Function UrlEncode(s) 
    UrlEncode = JSEngine.CodeObject.encodeURIComponent(s) 
    UrlEncode = Replace(UrlEncode, "'", "%27") 
    UrlEncode = Replace(UrlEncode, """", "%22") 
End Function 

Function UrlDecode(s) 
    UrlDecode = Replace(s, "+", " ") 
    UrlDecode = JSEngine.CodeObject.decodeURIComponent(UrlDecode) 
End Function 

WScript.Echo UrlDecode("Paris%20%E2%86%92%20Z%C3%BCrich") 
WScript.Echo UrlEncode("Paris → Zürich") 

3:他のWSHで

WSC Wizardを使用して作成)urlencdec.wsc

<?xml version="1.0"?> 
<component> 
<?component error="true" debug="true"?> 
    <registration 
     description="Url Encode/Decode Helper" 
     progid="JSEngine.Url" 
     version="1.0" 
     classid="{80246bcc-45d4-4e92-95dc-4fd9a93d8529}" 
    /> 
    <public> 
     <method name="encode"> 
      <PARAMETER name="s"/> 
     </method> 
     <method name="decode"> 
      <PARAMETER name="s"/> 
     </method> 
    </public> 
    <script language="JScript"> 
    <![CDATA[ 
     var description = new UrlEncodeDecodeHelper; 

     function UrlEncodeDecodeHelper() { 

      this.encode = encode; 
      this.decode = decode; 
     } 

     function encode(s) { 
      return encodeURIComponent(s).replace(/'/g,"%27").replace(/"/g,"%22"); 
     } 

     function decode(s) { 
      return decodeURIComponent(s.replace(/\+/g, " ")); 
     } 
    ]]> 
    </script> 
</component> 

とVBSコード:

Dim JSEngine 
Set JSEngine = GetObject("Script:C:\urlencdec.wsc") 

WScript.Echo JSEngine.decode("Paris%20%E2%86%92%20Z%C3%BCrich") 
WScript.Echo JSEngine.encode("Paris → Zürich") 
+2

これは素晴らしい回答です。ありがとうございます! –

+0

@ ft1よろしくお願いいたします。 –

+0

@ ft1修正をしましたが、2番目の解決策を使用した場合は、頭に入れてください。 –

-1

コードの最良の方法WSCを使用して、他のWSHでVBSを使用するURLは、encodeURIComponent()JavaScript関数を使用することです! ScriptControlコンポーネントを使用すると、VBS環境からjsコードを実行できます。 UTF-8で、URLDecode機能の

 
Function URLEncode(str) 
    Dim encodedUrl 
    Set sc = CreateObject("MSScriptControl.ScriptControl") 
    sc.Language = "JScript" 
    sc.AddCode "var s = """ & str & """;" 
    sc.AddCode "function myEncode(s){return encodeURIComponent(s);}" 
    urlCodificada = sc.Eval("myEncode(s);") 
    Set sc = Nothing 
    URLEncode = encodedUrl 
End Function 
2

ピュアVBS従来のASP:

は、ここで(実際には、それはそれを呼び出す!!)JS関数とまったく同じ動作します私のURLENCODE機能、ですサポート。

<% 
Function RegExTest(str,patrn) 
    Dim regEx 
    Set regEx = New RegExp 
    regEx.IgnoreCase = True 
    regEx.Pattern = patrn 
    RegExTest = regEx.Test(str) 
End Function 

Function URLDecode(sStr) 
    Dim str,code,a0 
    str="" 
    code=sStr 
    code=Replace(code,"+"," ") 
    While len(code)>0 
     If InStr(code,"%")>0 Then 
      str = str & Mid(code,1,InStr(code,"%")-1) 
      code = Mid(code,InStr(code,"%")) 
      a0 = UCase(Mid(code,2,1)) 
      If a0="U" And RegExTest(code,"^%u[0-9A-F]{4}") Then 
       str = str & ChrW((Int("&H" & Mid(code,3,4)))) 
       code = Mid(code,7) 
      ElseIf a0="E" And RegExTest(code,"^(%[0-9A-F]{2}){3}") Then 
       str = str & ChrW((Int("&H" & Mid(code,2,2)) And 15) * 4096 + (Int("&H" & Mid(code,5,2)) And 63) * 64 + (Int("&H" & Mid(code,8,2)) And 63)) 
       code = Mid(code,10) 
      ElseIf a0>="C" And a0<="D" And RegExTest(code,"^(%[0-9A-F]{2}){2}") Then 
       str = str & ChrW((Int("&H" & Mid(code,2,2)) And 3) * 64 + (Int("&H" & Mid(code,5,2)) And 63)) 
       code = Mid(code,7) 
      ElseIf (a0<="B" Or a0="F") And RegExTest(code,"^%[0-9A-F]{2}") Then 
       str = str & Chr(Int("&H" & Mid(code,2,2))) 
       code = Mid(code,4) 
      Else 
       str = str & "%" 
       code = Mid(code,2) 
      End If 
     Else 
      str = str & code 
      code = "" 
     End If 
    Wend 
    URLDecode = str 
End Function 


Response.Write URLDecode("Paris%20%E2%86%92%20Z%C3%BCrich") 'Paris → Zürich 
%> 
関連する問題