preg_replace_callback, carrying variables 
July 3rd, 2008, 03:55 PM
| | | |
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 | 
July 3rd, 2008, 09:05 PM
| | | | 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); | 
July 4th, 2008, 06:35 PM
| | | | 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);
>
>
>
| | 
July 4th, 2008, 07:15 PM
| | | | 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 | 
July 4th, 2008, 08:15 PM
| | | | 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
| |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|