Hi,
Is there a way to pass printf() an unknown number of arguments, for example this would be perfect for me (but obviously isn't valid)
myFunction('this is a %s containing two %s plus another %s');
function myFunction($myString)
{
$result = execQuery('SELECT f1,f2,f3 FROM table');
while($row = mysql_fetch_array($result)) {
printf($myString, for($i=0;$i<sizeof($row);$i++{ $row[$i] });
}
}
Okay, I know the above is invalid, I'm just trying to explain what I need. I want to be able to pass a string in to a function containing a number of %s tokens. I then want to printf the string using the fields in a recordset.
I always know that the string I pass in to the function has the correct number of %s for the number of fields returned in the recordset. However, it's not always the same number, in theory I could sizeof($row) and switch it, with 10 statements,
printf($myString, $row[0]);
printf($myString, $row[0], $row[1]);
printf($myString, $row[0], $row[1], $row[2]);
etc, I figure I'd only need a maximum of ten but it seems a bit stupid if there's another way. The only other thing I could think of is to not use printf and explode my string on %s then re-build it manually with echo substituting the var in the right place.
Any other hints?