Re: printf()
| From: | Danny Shepherd | Date: | Sun, 16 Jun 2002 00:06:03 +0000 |
| Subject: | Re: printf() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-102364@lists.php.net to get a copy of this message | ||
On Sunday 16 June 2002 12:46 am, Gerard Samuel wrote:
> Im trying to make a dynamic printf().
> By that I mean ->
>
> function this($foo, $bar)
> {
> if (strlen($bar) == '0')
> {
> print($foo);
> }
> else
> {
> printf($foo, $bar);
> }
> }
>
> Now it works if there is one argument passed, but it doesn't when there
> is more than one.
>
> $str = 'should';
> this('This %s work', $str); // work
>
> $str = 'is, a';
> this('This %s just %s test', $str); // doesn't work
>
> So I guess, arguments passed to it has to be physical arguments, and not
> represented in a string.
> Am I banging my head on a wall with this??
> Thanks
Try this :
<?php
function this($string,$params="")
{
if ($params!="")
{
eval("printf(\$string,$params);");
}
else
print $string;
return;
}
this('Hello, World :)<br />');
this('Hello %s<br />','"World"');
this('Hello %s %s<br />','"Wide", "World"');
?>