(PureBasic) URL Encoding and Decoding
Demonstrates URL encoding and decoding.
IncludeFile "CkStringBuilder.pb"
Procedure ChilkatExample()
; To URL encoding a string:
s.s = "Why a > b?"
sb.i = CkStringBuilder::ckCreate()
If sb.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success.i = CkStringBuilder::ckAppend(sb,s)
; URL encode the string.
CkStringBuilder::ckEncode(sb,"url","utf-8")
; Show the URL encoded string:
sEncoded.s = CkStringBuilder::ckGetAsString(sb)
Debug sEncoded
; The result is: Why%20a%20%3E%20b%3F
; If you prefer "+" instead of "%20" for SPACE chars:
numReplaced.i = CkStringBuilder::ckReplace(sb,"%20","+")
Debug CkStringBuilder::ckGetAsString(sb)
; Output is: Why+a+%3E+b%3F
; To decode:
CkStringBuilder::ckDecode(sb,"url","utf-8")
Debug CkStringBuilder::ckGetAsString(sb)
; Result is: Why a > b?
CkStringBuilder::ckDispose(sb)
ProcedureReturn
EndProcedure
|