URL ENCODE Function

Modified on 2016/12/17 19:53 by CaptainBoing — Categorized as: Encode and Decode, Network, Strings

Encodes a string so that it is acceptable as the argument part of a URL. Encoding is used when passing a string to a web server to avoid it being confused with the URL itself. It is normally used when the browser sends form data to a web server as part of the URI - when the form uses the GET method and the arguments appear as part of the web address.

Syntax:
URLEncode$(expression)

Example:
Arg$=URLEncode$(MyArg$)

Code:
FUNCTION URLEncode$(X$)
  LOCAL STRING A1$
  LOCAL INTEGER C,H
  FOR C=1 TO LEN(X$)
    H = ASC(MID$(X$, C, 1))
    IF H=32 THEN
      A1$ = A1$ + "+"
    ELSEIF (H>=48 AND H<=57) OR (H>=65 AND H<=90) OR (H>=97 AND H<=122) THEN
      A1$ = A1$ + CHR$(H)
    ELSE
      A1$ = A1$ + "%" + HEX$(H,2)
    END IF
  NEXT
  URLEncode$ = A1$
END FUNCTION