Click here to get back home

preg_replace_callback, carrying variables

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
preg_replace_callback, carrying variables Jeff 07-03-2008
Get Chitika Premium
Posted by Jeff on July 3, 2008, 10:49 am
Please log in for more thread options
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

Posted by Olaf Schinkel on July 3, 2008, 4:01 pm
Please log in for more thread options
Jeff schrieb:
> 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);




Posted by Michael Fesser on July 4, 2008, 2:11 pm
Please log in for more thread options
.oO(Jeff)

>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

Posted by Jeff on July 4, 2008, 3:14 pm
Please log in for more thread options
Michael Fesser wrote:
> .oO(Jeff)
>
>> 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

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

Similar ThreadsPosted
carrying over variables from a form to multiple pages... July 4, 2007, 11:17 pm
$_SESSION variable not carrying July 18, 2007, 3:11 pm
can you set FILES[] variables to session variables? October 17, 2006, 3:04 pm
php URL and variables July 12, 2004, 7:48 am
Re: Variables October 31, 2004, 7:56 pm
Variables June 9, 2005, 3:58 pm
variables June 17, 2005, 10:55 am
re-using variables February 27, 2007, 12:10 pm
Re: Getting rid of imported variables July 6, 2004, 5:18 am
using variables from form July 23, 2004, 12:53 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap