随笔 - 119  文章 - 0  评论 - 4  阅读 - 17万

如何在sprintf函数中输出百分号(%)等特殊符号

php中的sprinf可以格式化字符串的数据类型。今天遇到了想在其中输出%,可难倒我了。

$query =  sprintf("select * from books where %s like '% %s %'",$searchtype,$searchterm);

//我以为输出是这样的:select * from books where title like '% java %' ,但实际上输出会是select * from books where title like '% 

将其换成这样就可以了:

$query =  sprintf("select * from books where %s like '%%%s%%'",$searchtype,$searchterm);

//其中的%%中的第一个%用来转译特殊字符。

posted on   any91  阅读(9522)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示