note 84584 added to function.str-replace
| From: | xx dot kittychan dot xx at gmail dot com | Date: | Sun, 20 Jul 2008 16:08:13 +0000 |
| Subject: | note 84584 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142153@lists.php.net to get a copy of this message | ||
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)); }
?>
----
Server IP: 219.94.145.73
Probable Submitter: 122.17.129.25
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/84584
Del: integrated -- https://master.php.net/note/delete/84584/integrated
Del: useless -- https://master.php.net/note/delete/84584/useless
Del: bad code -- https://master.php.net/note/delete/84584/bad+code
Del: spam -- https://master.php.net/note/delete/84584/spam
Del: non-english -- https://master.php.net/note/delete/84584/non-english
Del: in docs -- https://master.php.net/note/delete/84584/in+docs
Del: other reasons-- https://master.php.net/note/delete/84584
Reject -- https://master.php.net/note/reject/84584
Search -- https://master.php.net/manage/user-notes.php