Connecting Tech Pros Worldwide Help | Site Map

preg_replace_callback, carrying variables

Jeff
Guest
 
Posts: n/a
#1: Jul 3 '08
I've been struggling with this for a while.

Here's what I'm trying to do:

private function makeMiniTemplate($ARRAY){

/*

$ARRAY looks like this:
$ARRAY['h']='Some Value';
$ARRAY['mce']='Something else';

*/


$template=<<<allthis
<div>[h]</div>
<div>[mce]</div>
....

allthis;

/*
I want to replace [h] with $ARRAY['h'];
Drives me crazy not to be able to use nowdocs in classes but that's
another story...
*/


$template=preg_replace_callback(
"/\[(.*?)\]/s",
create_function('$matches','echo $ARRAY[$matches[1]];'),
$template);

return $template;
}


So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped" inside
the create_function. It always comes up empty.

I don't see how to do this using callbacks either. I'm a little short of
understanding on how this works. I've tried a number of different
approaches.

How do I do this?

Jeff
Olaf Schinkel
Guest
 
Posts: n/a
#2: Jul 3 '08

re: preg_replace_callback, carrying variables


Jeff schrieb:
Quote:
I've been struggling with this for a while.
>
Here's what I'm trying to do:
>
private function makeMiniTemplate($ARRAY){
>
/*
>
$ARRAY looks like this:
$ARRAY['h']='Some Value';
$ARRAY['mce']='Something else';
>
*/
>
>
$template=<<<allthis
<div>[h]</div>
<div>[mce]</div>
...
>
allthis;
>
/*
I want to replace [h] with $ARRAY['h'];
Drives me crazy not to be able to use nowdocs in classes but that's
another story...
*/
>
>
$template=preg_replace_callback(
"/\[(.*?)\]/s",
create_function('$matches','echo $ARRAY[$matches[1]];'),
$template);
>
return $template;
}
>
>
So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped" inside
the create_function. It always comes up empty.
>
I don't see how to do this using callbacks either. I'm a little short of
understanding on how this works. I've tried a number of different
approaches.
>
How do I do this?
>
Jeff
Maybe Iīm blind :-) Or I donīt understand your question....
But to replace ALL [h] with $a:
$t = str_replace("[h]", $a, $t);



Jeff
Guest
 
Posts: n/a
#3: Jul 4 '08

re: preg_replace_callback, carrying variables


Olaf Schinkel wrote:
Quote:
Jeff schrieb:
Quote:
> I've been struggling with this for a while.
>>
> Here's what I'm trying to do:
>>
>private function makeMiniTemplate($ARRAY){
>>
>/*
>>
>$ARRAY looks like this:
>$ARRAY['h']='Some Value';
>$ARRAY['mce']='Something else';
>>
>*/
>>
>>
>$template=<<<allthis
><div>[h]</div>
><div>[mce]</div>
>...
>>
>allthis;
>>
>/*
>I want to replace [h] with $ARRAY['h'];
>Drives me crazy not to be able to use nowdocs in classes but that's
>another story...
>*/
>>
>>
>$template=preg_replace_callback(
> "/\[(.*?)\]/s",
> create_function('$matches','echo $ARRAY[$matches[1]];'),
> $template);
>>
>return $template;
>}
>>
>>
>So, I don't know how to pass in $ARRAY as $ARRAY is not "scoped"
>inside the create_function. It always comes up empty.
>>
>I don't see how to do this using callbacks either. I'm a little short
>of understanding on how this works. I've tried a number of different
>approaches.
>>
> How do I do this?
>>
> Jeff
>
Maybe Iīm blind :-) Or I donīt understand your question....
But to replace ALL [h] with $a:
I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...

Jeff
Quote:
$t = str_replace("[h]", $a, $t);
>
>
>
Michael Fesser
Guest
 
Posts: n/a
#4: Jul 4 '08

re: preg_replace_callback, carrying variables


..oO(Jeff)
Quote:
>I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...
Quick 'n dirty:

<?php
$template = 'some [foo] stuff';
$values = array(
'foo' ='bar'
);

function replace($matches) {
global $values;
$name = $matches[1];
return isset($values[$name]) ? $values[$name] : '';
}

$result = preg_replace_callback('/\[(.+?)]/', 'replace', $template);
var_dump($result);
?>

Micha
Jeff
Guest
 
Posts: n/a
#5: Jul 4 '08

re: preg_replace_callback, carrying variables


Michael Fesser wrote:
Quote:
.oO(Jeff)
>
Quote:
>I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...
>
Quick 'n dirty:
>
<?php
$template = 'some [foo] stuff';
$values = array(
'foo' ='bar'
);
>
function replace($matches) {
global $values;
Thanks. I had given up just prior to reading your post and had already
gone "global". I'd also determined that it couldn't be done without a
callback (at least I couldn't!).

Jeff
Quote:
$name = $matches[1];
return isset($values[$name]) ? $values[$name] : '';
}
>
$result = preg_replace_callback('/\[(.+?)]/', 'replace', $template);
var_dump($result);
?>
>
Micha
Closed Thread