/ Published in: ASP
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.
Makes for nice clean, understandable code as you can avoid concatenated strings containing function calls mid-string.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<% 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", "'", "''"))) %>