473,569 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_replace_ca llback, carrying variables

I've been struggling with this for a while.

Here's what I'm trying to do:

private function makeMiniTemplat e($ARRAY){

/*

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

*/
$template=<<<al lthis
<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_callbac k(
"/\[(.*?)\]/s",
create_function ('$matches','ec ho $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 1788
Jeff schrieb:
I've been struggling with this for a while.

Here's what I'm trying to do:

private function makeMiniTemplat e($ARRAY){

/*

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

*/
$template=<<<al lthis
<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_callbac k(
"/\[(.*?)\]/s",
create_function ('$matches','ec ho $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 makeMiniTemplat e($ARRAY){

/*

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

*/
$template=<<<a llthis
<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_callba ck(
"/\[(.*?)\]/s",
create_function ('$matches','ec ho $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($matche s) {
global $values;
$name = $matches[1];
return isset($values[$name]) ? $values[$name] : '';
}

$result = preg_replace_ca llback('/\[(.+?)]/', 'replace', $template);
var_dump($resul t);
?>

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($matche s) {
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_ca llback('/\[(.+?)]/', 'replace', $template);
var_dump($resul t);
?>

Micha
Jul 4 '08 #5

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

Similar topics

3
6265
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 home page that I need to make use of on other web pages - how can I do that? Thanks...
4
3775
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 about. def abc(): x = 1 y = x + 1 print y def abcd():
1
2069
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 suggestions. Thanks.
8
4468
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 in the object's lifetime (so there's no sense in making the callback function a method of the class...(is there??)) Instead, I wanted to use...
14
3965
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 use my backreferences the way I would like. (template.inc is not installed on my host.) If I'm approaching the problem wrong, please have mercy and...
2
13229
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 valid callback in ..." I tried both myclass::myfunction and $this->myfunction, neither worked. Is there really no way of using a class member as a...
1
5054
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 post to see if anyone can help me. what i need is to carry certain aspects of data from splash form to my main form which im currently developing. ...
0
1291
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 for a long time on a different server. Basically, I am reading an html page template into a variable with fread(). I then am doing this, which...
2
3564
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
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.