/* Takes a format string and an list of arguments and substitutes the format
codes in the format string for the elements of the arguments list. Similar to
vsprintf, but specialized for generating SQL queries.
Format codes are denoted by a percent sign (%) followed by another character. To
insert a literal percent character into the output string, escape it (%%). Each
successive format code is replaced by its corresponding argument in the list.
%%: Replaced by a literal percent sign.
%s: The argument, which must be a string, is used verbatim.
%S: Replaced by the argument, which is escaped with addslashes.
%q: Use for quoted strings. Like %S, but the value is also surrounded with quote
marks.
%l: Use for LIKE strings. Like %q, but the _ and % characters are also escaped.
%d: The argument, which should be numeric, is used verbatim. Non-numeric
arguments will be converted into numbers first.
%n: Like %q, but empty strings are replaced with NULL instead of ''.
%t: Like %n, but converts the result to a timestamp with FROM_UNIXTIME().
Parameters:
1. The format string.
2+. Any arguments needed by the format string.
*/
function format($format)
{
$argument = 0;
for ($i = strpos($format, '%'); $i !== false; $i = strpos($format, '%', $i + strlen($replace)))
{
$value = func_get_arg(++$argument);
switch (substr($format, $i + 1, 1))
{
case '%': $replace = '%'; --$argument; break;
case 's': $replace = $value; break;
case 'S': $replace = addslashes($value); break;
case 'q': $replace = format('"%S"', $value); break;
case 'l': $replace = preg_replace('/([%_\\\'"])/', '\\\\1', $value); break;
case 'd': $replace = (int)$value; break;
case 'n': $replace = (strlen($value) == 0 ? 'NULL' : format('%q', $value)); break;
case 't': $replace = format('FROM_UNIXTIME(%n)', $value); break;
default: $replace = '';
}
$format = substr_replace($format, $replace, $i, 2);
}
return $format;
}format('SELECT * FROM users WHERE id = %d AND userName = %q AND address LIKE "%%%l%%"', $id, $userName, $city)/* Takes a format string and an list of arguments and substitutes the format
codes in the format string for the elements of the arguments list. Similar to
vsprintf, but specialized for generating SQL queries.
Format codes are denoted by a percent sign (%) followed by another character. To
insert a literal percent character into the output string, escape it (%%). Each
successive format code is replaced by its corresponding argument in the list.
%%: Replaced by a literal percent sign.
%s: The argument, which must be a string, is used verbatim.
%S: Replaced by the argument, which is escaped with addslashes.
%q: Use for quoted strings. Like %S, but the value is also surrounded with quote
marks.
%l: Use for LIKE strings. Like %q, but the _ and % characters are also escaped.
%d: The argument, which should be numeric, is used verbatim. Non-numeric
arguments will be converted into numbers first.
%n: Like %q, but empty strings are replaced with NULL instead of ''.
%t: Like %n, but converts the result to a timestamp with FROM_UNIXTIME().
Parameters:
1. The format string.
2+. Any arguments needed by the format string.
*/
function format($format)
{
$argument = 0;
for ($i = strpos($format, '%'); $i !== false; $i = strpos($format, '%', $i + strlen($replace)))
{
$value = func_get_arg(++$argument);
switch (substr($format, $i + 1, 1))
{
case '%': $replace = '%'; --$argument; break;
case 's': $replace = $value; break;
case 'S': $replace = addslashes($value); break;
case 'q': $replace = format('"%S"', $value); break;
case 'l': $replace = preg_replace('/([%_\\\'"])/', '\\\\1', $value); break;
case 'd': $replace = (int)$value; break;
case 'n': $replace = (strlen($value) == 0 ? 'NULL' : format('%q', $value)); break;
case 't': $replace = format('FROM_UNIXTIME(%n)', $value); break;
default: $replace = '';
}
$format = substr_replace($format, $replace, $i, 2);
}
return $format;
}
posted by alphanerd at 3:22 PM on April 28, 2004