sprintf and arrays.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux