|
Posted by Chung Leong on 08/18/06 14:13
mens libertina wrote:
> Jerry Stuckle wrote:
> > mens libertina wrote:
> > > After I read your post, I declared $info as "$info = array();" at the
> > > top of the program with the same result. Did I misunderstand what you
> > > meant by global scope?
> >
> > But now your problem is $info in the function is not the same as $info
> > in the main program.
> >
> > When declared as a parameter like this, $info is only local to the
> > function. Any other $info is completely separate from the one in the
> > function.
>
> Ugh. So if I can't pass by reference, what options do I have? It
> sounds like I can't use preg_replace anywhere but the main body...
>
> <about to throw mouse!>
What you need to do is fake a closure by passing an object. Example
class KeywordContainer {
var $hash;
function KeywordContainer($hash) {
$this->hash = $hash;
}
function PCRESubstitute($m) {
$key = strtolower($m[1]);
if(array_key_exists($key, $this->hash)) {
return $this->hash[$key];
} else {
return $m[0];
}
}
}
$user = array('user' => "morgan",
'name' => "Morgan Seppy",
'expires' => "2006-08-28" );
$closure = array(new KeywordContainer($user), 'PCRESubstitute');
$template = "Hello, {NAME}! {USER} expires on {EXPIRES}";
echo preg_replace_callback('/\{(\w+?)\}/', $closure, $template);
Navigation:
[Reply to this message]
|