phpprintfescapingwildcardsql-like

How to escape percent symbols in the format parameter string of sprintf()


How do I add the SQL wildcard characters to this:

 sprintf("SELECT robot FROM robots WHERE robot LIKE '%s'",strtolower($user_agent));

as

 sprintf("SELECT robot FROM robots WHERE robot LIKE '%%s%'",strtolower($user_agent));

I need the %s placeholder to be wrapped in literal % characters without disrupting the placeholder.


Solution

  • A literal % is specifed as %%, so you want "... LIKE '%%%s%%'"