RE: [PHP] New feature suggestion: array in string substitution

From: Date: Wed, 29 Aug 2001 14:28:14 +0000
Subject: RE: [PHP] New feature suggestion: array in string substitution
References: 1  Groups: php.dev php.general 
Request: Send a blank email to php-general+get-65021@lists.php.net to get a copy of this message
have you looked at the eval function? it will let you do something similar: $string = 'this is a $variable'; $variable = 'template'; eval("\$string = $string"); echo $string; -> this is a template (haven't checked it, but syntax should be ok) jack -----Original Message----- From: --- [mailto:flagzen@libero.it] Sent: Wednesday, August 29, 2001 9:53 AM To: php-dev@lists.php.net; php-general@lists.php.net Subject: [PHP] New feature suggestion: array in string substitution The feature i suggest for inclusion in the php core is described below, especially in my first post. I think that is a really good idea and i ask you to spend a moment and read my suggestion. If you think that this feature could be useful, reply to this message and say that you agree (don't repeat all the text written below, i don't want to overload the news server) (my english is not very good, i'm sorry:) ---------------------------------------------------------- Federico Marani (flagzenNO-SPAM@libero.it) ---------------------------------------------------------- MY OLD POSTS: ----THE FIRST---- 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@libero.it ---------------- ---END FIRST POST--- ---THE SECOND POST--- Yes, the function you have written below make the same thing but i think that a function written in c, inside the php module, can be faster and more elegant than this php-function. Again, i think that the final user will be happy to have an already written function instead of think and code a personalized function. I think it's more elegant because it follow the same approach of printf-like functions and a user who already known these functions or known python will not have headaches in finding a solution for his problem Also prinf, sprinf, etc... can be written as php functions and separated from php core, but it will not happen... why? too important and too useful, they requires speed and their functionality is required in many programs. The same thing happen in python with dictionary substitution, and now, nobody think to remove it. I think that this type of function could be useful for a lot of people... Let me know what do you (also zeev and others) think about --------------------- Federico Marani flagzenNO-SPAM@libero.it ---------------------- Jo Giraerts <jo@life.be> wrote in message 20010828153504.A5406@life.be... > This is already possible in php, though with the following simple > function.. > > // function to read a file with php-vars in as a string > // $predefined_vars: an array ("varname" => "value"). all the > // variablenames defined in this array can be used in the bodyfile. > // They will receive the respective values. This makes personalising > // the mailes easier.. > function file_as_body($filename, $predefined_vars) > { > $ar = file($filename); > extract($predefined_vars); > > foreach ($ar as $number => $line) > { > eval("\$ar2[] = \"$line\";"); > } > > return implode("",$ar2); > } > > and for instance this file as template: > > <--- template file > You received a file on (" . date("d/m/Y (H:i)") . ") > > filename: ".basename($file) ." > filesize: ".filesize($file) ." > mimetype: $mimetype > > Hope you enjoy it.. > ---> > > You can even use php-code already, thanks to eval :) > > Maybe we better make it a PEAR-thing? > > > > ---END SECOND POST--- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#65021) next »