如何在PHP中实现类似printf的函数?
我想为我的数据库抽象制作一个如何在PHP中实现类似printf的函数?,php,printf,Php,Printf,我想为我的数据库抽象制作一个db_queryf函数。它的工作原理有点像SQLite中的sqlite3\u mprintf:db\u queryf('select*from page where name=%q',$\u GET['name']),其中%q将生成一个正确转义的字符串。在PHP中生成类似printf的函数的正确方法是什么?是否有任何帮助函数,或者我应该自己解析它? 非常重要的一点是,在使用$\u GET之前,您必须对其中的所有内容进行消毒 非常重要的一点是,在使用$\u GET之前
db_queryf
函数。它的工作原理有点像SQLite中的sqlite3\u mprintf
:db\u queryf('select*from page where name=%q',$\u GET['name'])
,其中%q将生成一个正确转义的字符串。在PHP中生成类似printf的函数的正确方法是什么?是否有任何帮助函数,或者我应该自己解析它?
非常重要的一点是,在使用$\u GET
之前,您必须对其中的所有内容进行消毒
非常重要的一点是,在使用
$\u GET
之前,您必须对其中的所有内容进行消毒 我很困惑(s)printf显然存在allready,您可能想为此使用更多,除非您想最终逃出/sql注入地狱。我很困惑<代码>(s)printf显然存在allready,您可能希望为此使用更多,除非您希望最终退出/sql注入地狱..使用。替换到字符串中不够好,应该进行消毒。使用。替换到字符串中还不够好,你应该进行消毒。好的,因为我有完全相同的问题,我尝试了一下,它似乎工作得很好
下面的函数位于数据库包装类中,希望像printf一样调用,其中%%
被转换为literal%,%e
将字符串参数标记为转义,而%u
将字符串参数标记为原样
LOGDB
是第二个数据库包装类,负责捕获和记录各种错误
public static function query($format)
{
$query = $format . ' ';
$argc = func_num_args();
$argv = func_get_args();
$index_query = 0;
$index_args = 1;
while (($index_query = strpos($query, '%', $index_query)) !== false)
{
switch ($query[$index_query + 1])
{
case '%':
$query = substr_replace($query, '', $index_query, 1);
$index_query++;
break;
case 'e':
if ($index_args >= $argc)
{
LOG::failedQuery($format, "not enough arguments for format");
return false;
}
$query = substr_replace($query, DB::escape($argv[$index_args]), $index_query, 2);
$index_query += strlen($argv[$index_args]);
$index_args++;
break;
case 'u':
if ($index_args >= $argc)
{
LOG::failedQuery($format, "not enough arguments for format");
return false;
}
$query = substr_replace($query, $argv[$index_args], $index_query, 2);
$index_query += strlen($argv[$index_args]);
$index_args++;
break;
default:
LOG::failedQuery($format, "unknown control sequence '%" . $query[$index_query + 1] . "'");
return false;
}
}
if ($index_args != $argc)
{
LOG::failedQuery($format, "too many arguments for format");
return false;
}
$res = mysqli_query(self::$handle, $query);
if (!$res)
LOGDB::failedQuery($query, mysqli_error(self::$handle));
return $res;
}
注意:代码大多未经测试,很可能包含一堆bug。小心使用:)好的,因为我有完全相同的问题,我尝试了一下,它似乎工作得很好
下面的函数位于数据库包装类中,希望像printf一样调用,其中%%
被转换为literal%,%e
将字符串参数标记为转义,而%u
将字符串参数标记为原样
LOGDB
是第二个数据库包装类,负责捕获和记录各种错误
public static function query($format)
{
$query = $format . ' ';
$argc = func_num_args();
$argv = func_get_args();
$index_query = 0;
$index_args = 1;
while (($index_query = strpos($query, '%', $index_query)) !== false)
{
switch ($query[$index_query + 1])
{
case '%':
$query = substr_replace($query, '', $index_query, 1);
$index_query++;
break;
case 'e':
if ($index_args >= $argc)
{
LOG::failedQuery($format, "not enough arguments for format");
return false;
}
$query = substr_replace($query, DB::escape($argv[$index_args]), $index_query, 2);
$index_query += strlen($argv[$index_args]);
$index_args++;
break;
case 'u':
if ($index_args >= $argc)
{
LOG::failedQuery($format, "not enough arguments for format");
return false;
}
$query = substr_replace($query, $argv[$index_args], $index_query, 2);
$index_query += strlen($argv[$index_args]);
$index_args++;
break;
default:
LOG::failedQuery($format, "unknown control sequence '%" . $query[$index_query + 1] . "'");
return false;
}
}
if ($index_args != $argc)
{
LOG::failedQuery($format, "too many arguments for format");
return false;
}
$res = mysqli_query(self::$handle, $query);
if (!$res)
LOGDB::failedQuery($query, mysqli_error(self::$handle));
return $res;
}
注意:代码大多未经测试,很可能包含一堆bug。小心使用:)db_查询的全部目的不是清理所有内容。例如,db_query('select*where name=%q',“'and 1=1--”);将生成select*其中name='\'和1=1--'。具有这样一个名称的页面显然不存在,因此查询不会造成任何伤害。db_查询的全部目的不是清理所有内容。例如,db_query('select*where name=%q',“'and 1=1--”);将生成select*其中name='\'和1=1--'。具有此名称的页面显然不存在,因此查询不会造成任何损害。您可能希望检查,并且,但是您必须解析查询字符串,搜索类似于占位符的%,并且它可能会干扰您希望使用类似“%foo%
查询的名称的位置。非常可行,而且被低估的问题。。。遗憾的是,答案毫无用处:(您找到问题的解决方案了吗?您可能想检查,并且,但是您必须解析查询字符串以搜索像占位符一样的%,这可能会干扰您想在哪里使用像“%foo%
查询这样的名称。非常可行,而且被低估的问题…很遗憾,答案是多余的。)你找到解决问题的办法了吗?