note 9663 modified in function.sprintf by erica

From: Date: Mon, 16 Sep 2002 00:14:10 +0000
Subject: note 9663 modified in function.sprintf by erica
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-36715@lists.php.net to get a copy of this message
Little note about sprintf and its 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 --was-- 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/en/function.sprintf.php

« previous php.notes (#36715) next »