Date: 08/28/01
- Next message: se <email protected>: "[PHP-DEV] Bug #12995: "Failed opening ... in Unknown on line 0"
- Previous message: ---: "[PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- In reply to: ---: "[PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Next in thread: James Moore: "Re: [PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Reply: James Moore: "Re: [PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I think that's a pretty good idea.
At 13:25 28-08-01, --- wrote:
>I have seen that in php there isn't nothing similar to dictionary
>substitution in python.
>(a dictionary is an array with string keys, like hash in perl)
>
>This change consist in adding two functions ("a" stay for "array"):
>aprintf(string format, array dict) -- like printf, print the result
>saprintf(string format, array dict) -- like sprintf, return the result
>
>It works like this (written in php-like language):
>
>format -> "my name is %(name)s and i'm %(age)s"
>dict -> array( name=>"tom", age=> "eighteen" );
>
>(in php, unlike python, is possible to make an array with both string and
>number indices, so the format can be also %(2)s,...)
>
>aprintf(format,dict) -- print "my name is tom and i'm eighteen"
>saprintf(format,dict) -- return "my name is tom and i'm eighteen"
>
>in python, these substitutions are very useful, especially in cgi
>programming, for making templates from text files, in php could be
>useful in, for example, language customisation, or message formatting,
>etc...
>
>An example:
>if ($lang == "it")
> define("MESSAGE","il %(animal)s %(color)s sta %(action)s %(target)s");
>else
> define("MESSAGE","the %(color)s %(animal)s is %(action)s");
>
>aprintf(MESSAGE,array(animal=>"cobra",color=>"green",action=>"eating",target
>=>"mouse"));
>// if the %(target)s isn't found, is ignored.
>
>
>(the "s" terminator could be substituted with other letters, like d for
>numbers, etc...)
>
>This approach has several advantages over something like this:
>"the $color $animal is $action"
>because in this phrase, variables are substituted when the parser execute
>it, and in this case:
>"the %(color)s %(animal)s is %(action)s"
>parameters are substituted only when the phrase is parsed with a specialized
>function like aprintf
>
>
>
>I think that this is a good idea and could save a lot of time when the
>program need to be as modular as possible.
>
>----------------
>Federico Marani
>flagzenNO-SPAM <email protected>
>----------------
>
>
>
>
>--
>PHP Development Mailing List <http://www.php.net/>
>To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
>For additional commands, e-mail: php-dev-help <email protected>
>To contact the list administrators, e-mail: php-list-admin <email protected>
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe <email protected> For additional commands, e-mail: php-dev-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: se <email protected>: "[PHP-DEV] Bug #12995: "Failed opening ... in Unknown on line 0"
- Previous message: ---: "[PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- In reply to: ---: "[PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Next in thread: James Moore: "Re: [PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Reply: James Moore: "Re: [PHP-DEV] python dictionary-like % (percent) substitution in php (was: Good idea in % (percent) substitutions in string)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

