Scripts
sprintf met array
Ik had iets nodig voor mijn talen systeem, maar daarbij werkte gewoon sprintf niet omdat ik alles in een array aangeleverd kreeg. Daarom heb ik deze functie geschreven Door blanche:================================== vervangen door:
sprintf-met-array
<?php
/**
* same as sprintf() except here you insert an array
*
* @author Iltar van der Berg
* @version 1.1.0
*
* @param string $input
* @param array $array
* @return string
*/
function sprintf_array($input, $array)
{
if(is_array($input))
{
die('<strong>sprintf_array:</strong> $input cannot be an array');
}
if(!is_array($array))
{
die('<strong>sprintf_array:</strong> $array must be an array');
}
if(count($array) == 0)
{
die('<strong>sprintf_array:</strong> $array must contain atleast 1 item');
}
$output = "\$output = sprintf('" . $input . "', '" . implode("', '", $array) . "');";
eval($output);
return $output;
}
// voorbeeld
$string = 'something like %s, is wrong. %s';
$array = array('this', 'right?');
$view = sprintf_array($string, $array);
echo $view;
?>
Reacties
0