Return to Snippet

Revision: 27563
at June 16, 2010 19:27 by samclarke


Initial Code
<%

Function SPrintF(sLine, aReplacements)
	
	aLines = Split(sLine, "%s")

	sFormatted = ""

	For i = 0 To UBound(aLines)
		If UBound(aReplacements) >= i Then
			sFormatted = sFormatted & aLines(i) & aReplacements(i)
		End If
	Next

	sFormatted = sFormatted & aLines(UBound(aLines))
	
	SPrintF = sFormatted

End Function

' example usage

Response.Write SPrintF("There are %s monkeys in the %s, but only %s lions in the %s.", _
						Array("five", "tree", "two", "grass"))

Response.Write SPrintF("Click here to visit <a href=""%s"">%s</a>!", _
						Array("http://www.google.com", Server.HTMLEncode("Google")))

Response.Write SPrintF("SELECT id, name FROM users WHERE username = '%s' AND password = MD5('%s');", _
						Array(Replace("sammy", "'", "''"), Replace("letmein", "'", "''")))

%>

Initial URL


Initial Description
This is a basic implementation of PHP's handy sprintf() written in Classic ASP/VBScript. It's not as extensive as PHP's version as it doesn't support numbered parameters, and only works with %s placeholders, but it's better than nothing, right?

Makes for nice clean, understandable code as you can avoid concatenated strings containing function calls mid-string.

Initial Title
Classic ASP (basic) implementation of PHP's sprintf() function

Initial Tags


Initial Language
ASP