Chris J Allen
Chris J Allen

Reputation: 19207

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.

Upvotes: 3

Views: 336

Answers (1)

Greg
Greg

Reputation: 321588

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

Upvotes: 15

Related Questions