URL2Text + Text2URL

URL2Text to convert a URL into string
Text2URL will converts a text with special chars into URL friendly string
ReplaceCriticalOnly = 1 means we do not want to do all chars, just the critical ones
Critical chars: characters that are needed to be replaced because otherwise might break the URL [space] ! " # ' / \
ReplaceCriticalOnly = 0 means we do want to do all special chars, except letters and numeric
Special chars: characters that we do not need to convert, will not break the URL
all Critical in addition to: $ [ ] ^ % & ( ) * + , - . : ; < = > ? @ _ { | } ~
Please be careful when converting ? & = most parameters are past using these urls, and some sites break when you convert them

Function URL2Text(sURL)
 ' Converts URL encoded into human text
 NV = Replace(sURL, "%20", " ")
 NV = Replace(NV, "%21", "!")
 NV = Replace(NV, "%22", Chr(34))
 NV = Replace(NV, "%23", "#")
 NV = Replace(NV, "%24", "$")
 NV = Replace(NV, "%25", "%")
 NV = Replace(NV, "%26", "&")
 NV = Replace(NV, "%27", "'")
 NV = Replace(NV, "%28", "(")
 NV = Replace(NV, "%29", ")")
 NV = Replace(NV, "%2A", "*")
 NV = Replace(NV, "%2B", "+")
 NV = Replace(NV, "%2C", ",")
 NV = Replace(NV, "%2D", "-")
 NV = Replace(NV, "%2E", ".")
 NV = Replace(NV, "%2F", "/")
 NV = Replace(NV, "%3A", ":")
 NV = Replace(NV, "%3B", ";")
 NV = Replace(NV, "%3C", "<")
 NV = Replace(NV, "%3D", "=")
 NV = Replace(NV, "%3E", ">")
 NV = Replace(NV, "%3F", "?")
 NV = Replace(NV, "%40", "@")
 NV = Replace(NV, "%5B", "[")
 NV = Replace(NV, "%5C", "\")
 NV = Replace(NV, "%5D", "]")
 NV = Replace(NV, "%5E", "^")
 NV = Replace(NV, "%5F", "_")
 NV = Replace(NV, "%7B", "{")
 NV = Replace(NV, "%7C", "|")
 NV = Replace(NV, "%7D", "}")
 NV = Replace(NV, "%7E", "~")
 URL2Text = NV
End Function

Function Text2URL(sText, ReplaceCriticalOnly)
 ' Converts a text with special chars into URL friendly string
 ' ReplaceCriticalOnly = 1 means we do not want to do all chars, just the critical ones
 ' Critical chars: characters that are needed to be replaced because otherwise might break the URL
 '  [space] ! " # ' / \
 ' ReplaceCriticalOnly = 0 means we do want to do all special chars, except letters and numerics
 ' Special chars: characters that we do not need to convert, will not break the URL
 '  all Criticals in addition to: $ [ ] ^ % & ( ) * + , - . : ; < = > ? @ _ { | } ~
 ' Please be careful when converting ? & = most parameters are past using these urls, and some sites break when you convert them
 '
 NV = Replace(sText, " ", "%20")
 NV = Replace(NV, "!", "%21")
 NV = Replace(NV, Chr(34), "%22")
 NV = Replace(NV, "#", "%23")
 NV = Replace(NV, "'", "%27")
 NV = Replace(NV, "/", "%2F")
 NV = Replace(NV, "\", "%5C")
 If ReplaceCriticalOnly = 1 Then
  NV = Replace(NV, "$", "%24")
  NV = Replace(NV, "[", "%5B")
  NV = Replace(NV, "]", "%5D")
  NV = Replace(NV, "^", "%5E")
  NV = Replace(NV, "%", "%25")
  NV = Replace(NV, "&", "%26")
  NV = Replace(NV, "(", "%28")
  NV = Replace(NV, ")", "%29")
  NV = Replace(NV, "*", "%2A")
  NV = Replace(NV, "+", "%2B")
  NV = Replace(NV, ",", "%2C")
  NV = Replace(NV, "-", "%2D")
  NV = Replace(NV, ".", "%2E")
  NV = Replace(NV, ":", "%3A")
  NV = Replace(NV, ";", "%3B")
  NV = Replace(NV, "<", "%3C")
  NV = Replace(NV, "=", "%3D")
  NV = Replace(NV, ">", "%3E")
  NV = Replace(NV, "?", "%3F")
  NV = Replace(NV, "@", "%40")
  NV = Replace(NV, "_", "%5F")
  NV = Replace(NV, "{", "%7B")
  NV = Replace(NV, "|", "%7C")
  NV = Replace(NV, "}", "%7D")
  NV = Replace(NV, "~", "%7E")
 End If
 Text2URL = NV
End Function

sURL
sText, ReplaceCriticalOnly

Views 2608 Downloads 880


ANmarAmdeen
590
Texts + Strings Classic ASP
Revisions

v1.0