Re: [PHP] sprintf and arrays.

2005-04-07 Thread Duncan Hill
On Wednesday 06 April 2005 18:05, Brent Baisley typed:
> You want to have sprintf look at the contents of the variable instead
> of the variable itself. Anytime you want to do something like this you
> use the eval() function to evaluate the contents of the variable.

Woot!

$begin_string = 'htmlentities(sprintf($tmp[0],';
foreach ($p_sub_values as $k => $v) {
 $mid_string[] = "\$p_sub_values[$k]";
}
$mid_string = join(',', $mid_string);
$end_string = '), ENT_QUOTES);';
// Eval will stick the three strings together and then execute them.  Neat 
trick, hint from
// [EMAIL PROTECTED]
eval ("\$r = $begin_string$mid_string$end_string");

Works a treat.  Thanks Brent.

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



Re: [PHP] sprintf and arrays.

2005-04-06 Thread Richard Lynch
On Wed, April 6, 2005 4:23 am, Duncan Hill said:
> 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);

As far as I know, there's no super easy way to do what you want for an
arbitrary number of arguments...

While PHP does have support for functions to take an arbitrary number of
arguments, there's no way to pass those on down to something else, like $@
in shell (and Perl?).  Least not as far as I know.

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

You could, of course, do something like:

list($a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10) = $tmp;
sprintf($a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, $a10);

Depending on your error reporting, you'll get messages, so you'll need to
use @ to suppress them.

I guess you could use count() and do string manipulation to build the
sprintf(...) you want and then use eval() and you could then avoid the
warnings...

Aha!

Wait a minute.

I *THINK* this will do what you want:

call_user_func_array('sprintf', $tmp);

Only I'm not sure you can use a built-in function for it...

http://php.net/call_user_func_array

Never used it, so not really sure it fits the bill.

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] sprintf and arrays.

2005-04-06 Thread Brent Baisley
Making life groovy has been difficult since the 60's.
You want to have sprintf look at the contents of the variable instead 
of the variable itself. Anytime you want to do something like this you 
use the eval() function to evaluate the contents of the variable.

On Apr 6, 2005, at 12:46 PM, Duncan Hill wrote:
On Wednesday 06 April 2005 16:40, Brent Baisley wrote:
For one, you are missing a right parenthesis ) in all of your 
examples.
htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )
Meh, syntax blip from the cut and paste.
Second, the string you are trying to format only has one variable
argument: $s.
Fred likes %1$s on his %2$s
The source array in this case has two entries.  My whole aim is to 
pass an
array of n entries, unroll the array into a string and somehow 
interpolate
the string into the sprintf call so that life is groovy.  I realise $s 
looks
like a single variable to PHP at that point sprintf() is called, and 
this is
what I'm trying to work around - some way to get sprintf to realise 
that $s
is actually two strings.  Ie, I need a double level of interpolation 
on the
$s value so that sprintf($tmp[0], $s) turns into sprintf(tmp[0], 
$array[0],
$array[1]).  I'm starting to get the feeling that this will be 
impossible.

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


Re: [PHP] sprintf and arrays.

2005-04-06 Thread Duncan Hill
On Wednesday 06 April 2005 16:40, Brent Baisley wrote:
> For one, you are missing a right parenthesis ) in all of your examples.
> htmlentities(  sprintf( $tmp[0], $s, ENT_QUOTES )

Meh, syntax blip from the cut and paste.

> Second, the string you are trying to format only has one variable
> argument: $s.
> Fred likes %1$s on his %2$s

The source array in this case has two entries.  My whole aim is to pass an 
array of n entries, unroll the array into a string and somehow interpolate 
the string into the sprintf call so that life is groovy.  I realise $s looks 
like a single variable to PHP at that point sprintf() is called, and this is 
what I'm trying to work around - some way to get sprintf to realise that $s 
is actually two strings.  Ie, I need a double level of interpolation on the 
$s value so that sprintf($tmp[0], $s) turns into sprintf(tmp[0], $array[0], 
$array[1]).  I'm starting to get the feeling that this will be impossible.

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



Re: [PHP] sprintf and arrays.

2005-04-06 Thread Brent Baisley
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