php sprintf() 使用中处理特殊字符 %

刚在使用 sprintf() 这个函数时发现字符串中有个特殊字符 %,字符串类似这样:

select * from boss where date_format(ftime_minute,'%Y-%m%-%d') >= '%s';

这里的如果不处理日期格式中的 % ,直接使用 sprintf() 就会出错,可以这样处理多加一个 %,如下:

$sql = " select * from boss where date_format(ftime_minute,'%%Y-%%m-%%d') >= '%s' ";
$txt = sprintf($sql, "2019-12-12");
echo $txt;

结果:
在这里插入图片描述

参考
https://www.w3schools.com/php/func_string_sprintf.asp

发布了145 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xiaobinqt/article/details/103633544