This function is useful when passing URL query strings which might need to be encoded to preserve them. Corresponds to the
'application/x-www-form-urlencoded' media type
'application/x-www-form-urlencoded' media type
Code:
FUNCTION UrlEncode( BYVAL sToEscape AS STRING ) AS STRING 'formats a string to comply with CGI application/x-www-form-urlencoded 'media type. Useful in CGI to be used as a query part or to pass 'variables around REGISTER lIterIn AS LONG REGISTER lIterOut AS LONG LOCAL lLength AS LONG LOCAL pbByteIn AS BYTE PTR LOCAL pbByteOut AS BYTE PTR LOCAL pszReturnStr AS ASCIIZ PTR LOCAL sOut AS STRING LOCAL sHex AS STRING * 2 lLength = LEN(sToEscape) 'Allocate to len(sToEscape) * 3 + 1 (worst case scenario) sOut = SPACE$( (lLength * 3)+1 ) pbByteIn = STRPTR( sToEscape ) pbByteOut = STRPTR( sOut ) DO UNTIL lIterIn = lLength SELECT CASE @pbByteIn[lIterIn] CASE 48 TO 57, 65 TO 90, 97 TO 122 @pbByteOut[lIterOut] = @pbByteIn[lIterIn] CASE 32 @pbByteOut[lIterOut] = 43 '+ CASE ELSE IF @pbByteIn[lIterIn] < 16 THEN @pbByteOut[lIterOut] = 37 '% @pbByteOut[lIterOut+1] = 48 '0 @pbByteOut[lIterOut+2] = ASC(HEX$(@pbByteIn[lIterIn])) ELSE @pbByteOut[lIterOut] = 37 '% sHex = HEX$(@pbByteIn[lIterIn]) @pbByteOut[lIterOut+1] = ASC(sHex,1) @pbByteOut[lIterOut+2] = ASC(sHex,2) END IF lIterOut = lIterOut + 2 END SELECT INCR lIterIn INCR lIterOut LOOP @pbByteOut[lIterOut] = 0 'terminate the string pszReturnStr = STRPTR( sOut ) FUNCTION = @pszReturnStr END FUNCTION