variable scope / preg_replace_callback question
| From: | Peter Bowyer | Date: | Mon, 03 Dec 2001 13:45:49 +0000 |
| Subject: | variable scope / preg_replace_callback question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-76375@lists.php.net to get a copy of this message | ||
Hi,
Does anyone know of a way of passing other variables to the function being called by
preg_replace_callback? For instance, I have the following code:
function smarty_compile_lang($tpl_source) {
// en.php contains a very large array, $_lang
include_once '/home/test/en.php';
global $_lang;
return preg_replace_callback('/##(.+?)##/', '_compile_lang', $tpl_source);
}
function _compile_lang($key){
// If I include $_lang here then it gets
// loaded again and again - bad for performance as it is _very_ large
return $_lang[$key[1]];
} // End _compile_lang
Somehow I need to make $_lang global, or pass it through the function preg_replace_callback as an
arguement for _compile_lang() - which I don't think is possible?
What can I do? I've tried a global statement everywhere I can think of... and I don't
want to include en.php outside the functions, as it'll be included then whether the templates
are being compiled or not. I'm
guessing that I may want to look at a higher level of abstraction from these functions, as even if I
have the include in the current place, it means that every time a template is compiled it gets
reloaded (though that's
not as bad as every time preg_replace_callback() finds something) whereas the best way would be to
load it once per script invocation - and then only when the templates wer being compiled...
Suggestions?
TIA,
Peter.