Re: sprintf and arrays.

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

 



For one, you are missing a right parenthesis ) in all of your examples.
htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )

Second, the string you are trying to format only has one variable argument: $s.
Fred likes %1$s on his %2$s
You have it numbered for ordering, but you are still ordering one variable. I never used sprintf like that, so I'm not sure if that is a valid way of doing it. That may be what is giving you the too few arguments error. Typically you would do something like:
Fred likes %1$s on his %2$t
If you are using sprintf in a valid manner, then the problem is probably in the $s argument of the sprintf function. You are only specifying one variable to replace when you are looking to do two replacements. The contents of $s is a single string that happens to contains commas, it does not get evaluated as multiple arguments just because it contains commas. Try wrapping $s in eval().
htmlentities( sprintf( $tmp[0], eval($s) ), ENT_QUOTES);


Personally, I rolled my own basic search and replace string function to support my own templating "tag" system. I still use sprinf for more "fancy" stuff, but for basic stuff, I rolled my own. A simplified version is below. It accepts an associative array and a string as parameters and returns the merged result.

$text_str = "And example for {::Name::} in answer to {::Question::} on the {::ListName::}. Hope it helps {::Name::}!";
$data_Merge['Name'] = "Duncan Hill";
$data_Merge['Question'] = "sprintf and arrays";
$data_Merge['ListName'] = "PHP General";
echo mergeTplData($data_Merge, $text_str);


function mergeTplData($data, $str) {
	$searchTags = array_keys($data);
	$searchTags = '{::'.implode('::},{::',$searchTags).'::}';
	$searchTags = explode(',',$searchTags);
	$str = str_replace($searchTags,$data,$str);
	return $str;
}

On Apr 6, 2005, at 7:23 AM, Duncan Hill wrote:

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


--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

--
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