note 84584 deleted from function.str-replace by danbrown

From: Date: Sun, 20 Jul 2008 16:12:56 +0000
Subject: note 84584 deleted from function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142163@lists.php.net to get a copy of this message
Note Submitter: xx dot kittychan dot xx at gmail dot com ---- I've never seen it called interpolate before, but on a whim I decided to make another one-liner for myself. Somehow it turned out very much like Max's version. jaimthorn, your version also uses array_keys and array_values, which is basically like the other half of Max's function. I only used array_map because it returns the input array, unlike array_walk, both of which are doing the same thing (evaluating elements of array against a provided function). And if you don't think create_function is used a lot, head over to any of the functional languages, where anonymous functions are an integral part of the language, or even to Javascript, where every second line looks like this: var THAT = function () { /* code goes here */ } Anyhow, to the one-liner: (test.tmpl) hello %abc% replace %def% lol <?php $test = array("abc" => 9000, "def" => "hello?"); function _tmpl($arr, $tmpl) { return str_replace(array_map(create_function('$a', 'return "%$a%";'), array_keys($arr)), array_values($arr), file_get_contents($tmpl)); } ?>

« previous php.notes (#142163) next »