note 9663 added to sprintf
| From: | voudras at nospam dot swiftslayer dot org | Date: | Fri, 17 Nov 2000 11:58:21 +0000 |
| Subject: | note 9663 added to sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-7809@lists.php.net to get a copy of this message | ||
Little note about sprintf and it's ilk.
if you attempt something like
$string = "dingy%sflem%dwombat";
$nbr = 5;
$name = "voudras";
$msg = sprintf("%d $string %s", $nbr, $name);
sprintf will complain about a lack in the number of arguments, this would be because of the %'s
in the actual string. This can be a great benifit, but is also rather confusing if you dont realize
this feature, and are passing questionable variables to sprintf (for, say perhaps logging). One way
around this is using
ereg_replace("%","%%", $string); before
sending it off to sprintf. This is actually how i came across this as a problem - i had realized
some time ago that i would have to test my $string for
%'s, but when running the %->%% replacement on a very large serialized object, my
application timed out.
My solution was to use
sprintf("%d %s %s", $nbr, $string, $name);
but, there was a reason i originally had done this the other way - i suppose i'll find out soon
enough
http://www.php.net/manual/function.sprintf.php