ASP String Functions
Since I have been working a lot with ASP lately, I have decided to create a few posts with some functions I have created to solve common issues. The first post in the series is regarding Strings.
The first code snippet is a little function that converts text into mixed case, or sentance case. It basically seeks out spaces in a string and converts the first character after thhe space to a capitalized letter and the remaining characters before the next space to lower case.
<%
Function MixedCase(strInput)
Dim strPos, strSpace, strOutput
' Set our position variable to the start of the string.
strPosition = 1
' Loop to check spaces
Do While InStr(strPosition, strInput, " ", 1) <> 0
strSpace = InStr(strPosition, strInput, " ", 1)
strOutput = strOutput & UCase(Mid(strInput, strPosition, 1))
strOutput = strOutput & LCase(Mid(strInput, strPosition + 1, strSpace - strPosition))
strPosition = strSpace + 1
Loop
' Last word is currently uncapitalized - fix this
strOutput = strOutput & UCase(Mid(strInput, strPosition, 1))
strOutput = strOutput & LCase(Mid(strInput, strPosition + 1))
MixedCase = strOutput
End Function
%>
ASP seems to lack a URL Decode function but has a URL Encode function available. Here is a function that can decode any URL Encoded URL or variable. I found this script a while ago on another site and have used it when necessary.
<%
Function URLDecode(str)
Dim re
Set re = new RegExp
str = Replace(str, "+", " ")
re.Pattern = "%([0-9a-fA-F]{2})"
re.Global = True
URLDecode = re.Replace(str, GetRef("URLDecodeHex"))
end function
' Replacement function for the above
Function URLDecodeHex(match, hex_digits, pos, source)
URLDecodeHex = chr("&H" & hex_digits)
End Function
%>
Here is a practical example of when you would use the URLDecode function – to record the search terms someone has use to find your site in Google. This functions examines the string to see if it contains “google.” and if it does, assumes the string contains google keywords and seeks out “q=” inside the string. If it also finds this, the stripStr is now created with the prefix “GOOGLE:” and the key words are appended to the end.
<%
FUNCTION StripGoogleTerms(stripStr)
stripStr = URLDecode(stripStr)
IF InStr(stripStr, "google.") THEN 'Google Refer
WordArray = Split(stripStr, "&")
FOR i = LBound(WordArray) TO UBound(WordArray)
pos = InStr(WordArray(i), "q=")
IF pos <> 0 THEN
tempStr = "GOOGLE:" & MID(WordArray(i),pos+2)
END IF
NEXT
ELSE 'do nothing
tempStr = stripStr
END IF
StripGoogleTerms = tempStr
END FUNCTION
%>
This string function is a function that returns the name of the script/asp page currently being browsed, minus the path name and the .asp at the end. I find this very useful when used inside a header include file where you wish to have ‘dynamically inserted’ title tags, meta tags and the like.
<%
FUNCTION GetScriptLocation(callerURL,partURL)
'Split the path along the /s. This creates a one-dimensional array
arPath = Split(callerURL, "/")
' The last item in the array contains the file name
script_name = arPath(UBound(arPath,1))
script_name = Replace(script_name,".asp","")
GetScriptLocation = script_name
END FUNCTION
%>
Here is an example of the above function used in the method that I mentioned before. It is very useful to make your include files dynamic so you don’t have duplicate header information on every file, just because you share the same header include. This assists with basic SEO and ensuring each page is different, which is sometimes a problem on dynamic sites.
<%
scriptName = GetScriptLocation(Request.ServerVariables("script_name"))
siteName = "My Site"
SELECT CASE script_name
CASE "default"
MainHeading = "Home Page"
keywords = "home page, welcome"
descript = "Welcome to my home page"
CASE "about-us"
MainHeading = "About Us"
keywords = "about us"
descript = "Find out more about us"
%>
<html>
<head>
<TITLE><%=siteName%> :: <%=MainHeading%></TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META name="Keywords" content="<%=keywords%>">
<META name="Description" content="<%=descript%>">
....
The final string function is a short and sweet function that will strip HTML tags out of any string. It uses regex expressions and is very fast.
<%
FUNCTION stripHTML(strHTML)
Dim objRegExp, strOutput, tempStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<(.|n)+?>"
'Replace all HTML tag matches with the empty string
strOutput = objRegExp.Replace(strHTML, "")
'Replace all < and > with < and >
strOutput = Replace(strOutput, "<", "<")
strOutput = Replace(strOutput, ">", ">")
stripHTML = strOutput 'Return the value of strOutput
Set objRegExp = Nothing
END FUNCTION
I hope you find these functions as useful as I do.