sprintf and arrays.
| From: | Duncan Hill | Date: | Wed, 06 Apr 2005 11:23:31 +0000 |
| Subject: | sprintf and arrays. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-212398@lists.php.net to get a copy of this message | ||
I have a snippet of code that looks something like:
if (is_array($p_sub_values)) {
foreach ($p_sub_values as $i => $v) {
$p_sub_values_str[$i] = "'$v'";
}
$s = join(',', $p_sub_values_str);
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
}
$tmp[0] in this case contains a string like 'Fred likes %1$s on his %2$s',
taking advantage of positional substitution with sprintf.
The function call to this snippet can have an optional array passed. My
need/desire is to substitute each element of the array into the appropriate
position with sprintf. So far I've tried:
$r = htmlentities(sprintf($tmp[0], $s, ENT_QUOTES);
$r = htmlentities(sprintf($tmp[0], ${$s}, ENT_QUOTES);
and a few other bits and pieces, all to no avail (error is about not enough
arguments).
Is there any way to accomplish this in PHP, or do I need to roll my own
substitution code? The array can obviously be anything from a single value
to 'unlimited' (though in practice will probably be less than 5).
--
My mind not only wanders, it sometimes leaves completely.