[已解决]求URL编码 特殊字符转换问题
本帖最后由 xwei21 于 2019-11-9 17:52 编辑我采用afan的url编码函数,但是遇到特殊字符 : / 等无法转换为 %3A %2F 等,应该怎样修改函数才能转换这些特殊字符,求教高手,谢谢
MsgBox(32, "", _UnicodeURLEncode('/成都:'))
Func _UnicodeURLEncode($UnicodeURL)
Local $UnicodeBinaryLength, $UnicodeBinary, $UnicodeBinary2
$UnicodeBinary = StringToBinary($UnicodeURL, 4)
$UnicodeBinary2 = StringReplace($UnicodeBinary, '0x', '', 1)
$UnicodeBinaryLength = StringLen($UnicodeBinary2)
Local $EncodedString, $UnicodeBinaryChar, $EncodedString
For $i = 1 To $UnicodeBinaryLength Step 2
$UnicodeBinaryChar = StringMid($UnicodeBinary2, $i, 2)
If StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", BinaryToString('0x' & $UnicodeBinaryChar, 4)) Then
$EncodedString &= BinaryToString('0x' & $UnicodeBinaryChar)
Else
$EncodedString &= '%' & $UnicodeBinaryChar
EndIf
Next
Return $EncodedString
EndFunc
本帖最后由 w60711 于 2019-11-9 16:36 编辑
这个也无法...qq
$sStr = '/成都:'
$encode = _Conversion($sStr,0)
$decode = _Conversion($encode,1)
MsgBox(0,'encodeURI',$encode)
MsgBox(0,'decodeURI',$decode)
Func _Conversion($string,$flg)
$oSC = ObjCreate("ScriptControl")
$oSC.language = "JavaScript"
If $flg = 0 Then
Return $oSC.eval("encodeURI('" & $string & "');")
Else
Return $oSC.eval("decodeURI('" & $string & "');")
EndIf
EndFunc
本帖最后由 w60711 于 2019-11-9 16:42 编辑
嗯...我搞错意思了
你应该是要这样?
MsgBox(0,0,_StringToEncode("/成都:",1,1))
Func _StringToEncode($str, $Encode = 1, $AllFlag = 0, $sPrefix = '%')
;afan 提示:转换字符串为ANSI;GB2312;URL;unicode等格式编码
;$str - 需转换的字符串
;$Encode - [可选参数]编码格式: 1(默认) = ANSI; 2 = UTF16 小; 3 = UTF16 大; 4 = UTF8
;$AllFlag - [可选参数] =0 不转换字母及数字(默认); =1 全部转换(字符头http://除外)
;$sPrefix - [可选参数]编码首字符
;返回值:成功 - 返回转换后的编码字符串; 失败 - 设置 @Error = 1
Local $str_s = StringRegExpReplace($str, '^\s*(?i)(http://).+', '$1')
If Not @extended Then $str_s = ''
If @extended Then $str = StringRegExpReplace($str, '^\s*(?i)http://', '')
Local $sOut, $i, $sS2B, $aSR
If $AllFlag Then
$sS2B = StringTrimLeft(StringToBinary($str, $Encode), 2)
$sOut = StringRegExpReplace($sS2B, '..', $sPrefix & '$0')
Else
$sOut = $str
$aSR = StringRegExp($str, '\W+', 3)
If Not @Error Then
For $i = 0 To UBound($aSR) - 1
$sS2B = StringTrimLeft(StringToBinary($aSR[$i], $Encode), 2)
$sOut = StringReplace($sOut, $aSR[$i], StringRegExpReplace($sS2B, '..', $sPrefix & '$0'), 1)
Next
EndIf
EndIf
If $sPrefix = ' ' Then $sOut = StringRegExpReplace($sOut, '^\h+|\h+
, '')
;$sOut = StringReplace($sOut, '%20', '+') ;转换空格的编码为'+'号,也可不要此行
Return $str_s & $sOut
EndFunc ;==>_StringToEncode
非常感谢,困扰我很久的问题解决了。
页:
[1]