Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 3rd, 2008, 03:55 PM
Jeff
Guest
 
Posts: n/a
Default preg_replace_callback, carrying variables

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
  #2  
Old July 3rd, 2008, 09:05 PM
Olaf Schinkel
Guest
 
Posts: n/a
Default 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);



  #3  
Old July 4th, 2008, 06:35 PM
Jeff
Guest
 
Posts: n/a
Default 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);
>
>
>
  #4  
Old July 4th, 2008, 07:15 PM
Michael Fesser
Guest
 
Posts: n/a
Default 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
  #5  
Old July 4th, 2008, 08:15 PM
Jeff
Guest
 
Posts: n/a
Default 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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles