On Mon, 4 Oct 2004, Michael Sims wrote: > Chuck Wolber wrote: > > The method I've come up with in the meantime, I believe is much more > > effective than heredocs, but still an ugly hack: > > > > function interpolate ($text, $msg_variable) { > > $msg_key = '_FP_VAR_'; > > > > foreach (array_keys($msg_variable) as $key) { > > $token = $msg_key.$key; > > $text = preg_replace("/$token/", "$msg_variable[$key]", > > $text); } > > > > return ($text); > > } > > What's ugly about it? I saw your earlier post I was actually planning > on responding and suggesting something exactly like you just came up > with. The main problem (aside from performance, which you addressed) is that it does not handle corner cases such as when you want to use one of your variables within the text of the dispatch (escaping). It is also not truly recursive. >From a hacker standpoint, it extends the base PHP tool set, rather than making use of existing tools (not necessarily a bad thing). IMHO a non-ugly version of this would allow you to use standard PHP variables in your dispatch and force PHP itself to re-interpret the variables. This is why I think an interpolate() function would have to be built in to the base PHP interpreter rather than be done with the language itself. > I think it's fairly clean and logical. Thank you :) > The only problem I can see with this approach is that it's inefficient > as $msg_variable gets larger or the $text gets larger, since you're > iterating through every key of the former, even if the token isn't in > $text, and you run preg_replace() once for each token, but that > shouldn't hurt you unless you really have a heavy traffic site, IMHO. Right, which is another reason my hack shouldn't be considered a generalized solution to the recursive interpolation problem. Any developers on the list want to comment on a possible interpolate() function? > I toyed around with using preg_replace_callback() for this eariler, but > the only problem with it is that you can't create a callback that > accepts more than just one variable (the array of matches). This means > I couldn't get the equivalent of your $msg_variable passed to the > callback, so I had to make it global in the callback function (yuk). > If it weren't for that it'd be a perfect solution, because > preg_replace_callback would only be called once and the function > wouldn't need to iterate through all of $msg_variable. It's times like > that that I miss Perl's more powerful variable scoping rules. Ah > well... Definitely cleaner and even appears (at first glance) that it lends itself to being recursive a lot easier than mine. Could you solve the global problem by doing a $this.msg_variable sort of thing within the callback? -Chuck -- http://www.quantumlinux.com Quantum Linux Laboratories, LLC. ACCELERATING Business with Open Technology "The measure of the restoration lies in the extent to which we apply social values more noble than mere monetary profit." - FDR -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php