473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 3 '08 #1
4 1777
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);

Jul 3 '08 #2
Olaf Schinkel wrote:
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:
I'd like to replace [h] with $a['h'], similarly [p] with $a['p']...

Jeff
$t = str_replace("[h]", $a, $t);
Jul 4 '08 #3
..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
Jul 4 '08 #4
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
Jul 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I was reading about PHP variable scoping - it doesn't seem to support carrying any variables from one web page to another - how is this usually done? I have a lot of php variables created on my...
4
by: Ivan Shevanski | last post by:
Alright heres my problem. . .Say I want to carry over a variable from one function to another or even another run of the same function. Is that possible? Heres a quick example of what I'm talking...
1
by: Ani | last post by:
Hi, I need to carry the user input across pages and then at the end insert all the values into the DB. How do I best accomplish this task in ASP.Net. I am a novice , please give me some simple...
8
by: jody.florian | last post by:
Hi, I'm trying to use preg_replace_callback within a method. The preg_replace_callback() & mycallback() pair will only be used by this method, and this method will probably only be called once...
14
by: mens libertina | last post by:
Disclaimer: In addition to reading the skimpy entries at php.net, I've searched this group and alt.php to no avail. Basically, I'm trying to use a template to send email reminders, but I can't...
2
by: Kimmo Laine | last post by:
I tried to use a class member function as a callback in preg_replace_callback, but failed. It announces: " Warning: preg_replace_callback() : requires argument 2, 'myclass::myfunction', to be a...
1
devonknows
by: devonknows | last post by:
Hi, im having trouble carrying variables across a form, ive looked on here and other sites but cant find anything that helps me, or i might not be searching for the right terms, so i though i would...
0
by: Stephen Kay | last post by:
I'm having a problem where I changed hosts, went from php 4.1.2 to php 4.4.4, and now preg_replace_callback() is segfaulting when I pass it html that is longer than 2732 bytes. This code has worked...
2
by: Snaggy | last post by:
this is my problem: Class myClass { function process($matches) {return something($matches);} function myReplace($input) { return preg_replace_callback(/pattern/, '$this->process', $input)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.