You are here: sprintf and arrays. « PHP « IT news, forums, messages
sprintf and arrays.

Posted by Duncan Hill on 04/06/05 14:23

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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация