473,748 Members | 6,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing template using preg_replace_ca llback()

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 tell me how to solve it! :)

I have an array, $info, that looks like this (this is just one user):
Array ( [morgan] =Array
(
[user] =morgan
[name] =Morgan Seppy
[expires] =2006-08-28
)
)

and a sample template file:
Hello, {NAME}!

This is a courtesy reminder that your account, {USER}, is due to
expire on {EXPIRES}.

Please let us know if you need assistance or have questions.

Thanks,
Staff

So, I want to replace the {TAG} with the appropriate item in $info. the
relevant code that doesn't work is (i've deleted my debugging print
statements):

function CreateEmail( $info )
{
$emesg = file_get_conten ts( TMPLFILE );
if(!$emesg)
{ error_log( "Template file, ".TMPLFILE. ", not read.\n", 3,
$errfile ); }
else
{
$tagpatt = '/\{(\w+?)\}/';
$emesg = preg_replace_ca llback( $tagpatt,
create_function ( '$matches, $info',
'$field=strtolo wer($matches[1]);return
$info[$field];' ),
$emesg );
}
}

result:
Warning: Missing argument 2 for __lambda_func() in
/home/content/c/a/j/cajtech/html/awkwords/admin/remind.php(39) :
runtime-created function on line 1

It seems that preg_replace callback() doesn't know that $info exists.

I've also tried just using backrefences, but I couldn't manipulate them
(writing this from memory):
$emesg = preg_replace( $tagpatt, eval( "$info[strtolower("."$ 1".")]"),
$emesg );
Thanks

Aug 18 '06 #1
14 3981
mens libertina wrote:
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 tell me how to solve it! :)

I have an array, $info, that looks like this (this is just one user):
Array ( [morgan] =Array
(
[user] =morgan
[name] =Morgan Seppy
[expires] =2006-08-28
)
)

and a sample template file:
Hello, {NAME}!

This is a courtesy reminder that your account, {USER}, is due to
expire on {EXPIRES}.

Please let us know if you need assistance or have questions.

Thanks,
Staff

So, I want to replace the {TAG} with the appropriate item in $info. the
relevant code that doesn't work is (i've deleted my debugging print
statements):

function CreateEmail( $info )
{
$emesg = file_get_conten ts( TMPLFILE );
if(!$emesg)
{ error_log( "Template file, ".TMPLFILE. ", not read.\n", 3,
$errfile ); }
else
{
$tagpatt = '/\{(\w+?)\}/';
$emesg = preg_replace_ca llback( $tagpatt,
create_function ( '$matches, $info',
'$field=strtolo wer($matches[1]);return
$info[$field];' ),
$emesg );
}
}

result:
Warning: Missing argument 2 for __lambda_func() in
/home/content/c/a/j/cajtech/html/awkwords/admin/remind.php(39) :
runtime-created function on line 1

It seems that preg_replace callback() doesn't know that $info exists.
It wouldn't. $info is local to this function. You need to have a
variable with global scope - or at least one which is available to the
main code, not just a function.
I've also tried just using backrefences, but I couldn't manipulate them
(writing this from memory):
$emesg = preg_replace( $tagpatt, eval( "$info[strtolower("."$ 1".")]"),
$emesg );
Thanks

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 18 '06 #2

Jerry Stuckle wrote:
>
It wouldn't. $info is local to this function. You need to have a
variable with global scope - or at least one which is available to the
main code, not just a function.
After I read your post, I declared $info as "$info = array();" at the
top of the program with the same result. Did I misunderstand what you
meant by global scope?

Thanks.

Aug 18 '06 #3
mens libertina wrote:
Jerry Stuckle wrote:
>>It wouldn't. $info is local to this function. You need to have a
variable with global scope - or at least one which is available to the
main code, not just a function.


After I read your post, I declared $info as "$info = array();" at the
top of the program with the same result. Did I misunderstand what you
meant by global scope?

Thanks.
But now your problem is $info in the function is not the same as $info
in the main program.

When declared as a parameter like this, $info is only local to the
function. Any other $info is completely separate from the one in the
function.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 18 '06 #4

Jerry Stuckle wrote:
mens libertina wrote:
After I read your post, I declared $info as "$info = array();" at the
top of the program with the same result. Did I misunderstand what you
meant by global scope?

But now your problem is $info in the function is not the same as $info
in the main program.

When declared as a parameter like this, $info is only local to the
function. Any other $info is completely separate from the one in the
function.
Ugh. So if I can't pass by reference, what options do I have? It
sounds like I can't use preg_replace anywhere but the main body...

<about to throw mouse!>

Aug 18 '06 #5
mens libertina wrote:
Jerry Stuckle wrote:
>>mens libertina wrote:
>>>After I read your post, I declared $info as "$info = array();" at the
top of the program with the same result. Did I misunderstand what you
meant by global scope?

But now your problem is $info in the function is not the same as $info
in the main program.

When declared as a parameter like this, $info is only local to the
function. Any other $info is completely separate from the one in the
function.


Ugh. So if I can't pass by reference, what options do I have? It
sounds like I can't use preg_replace anywhere but the main body...

<about to throw mouse!>
No, you can also make it a global in the function and not pass it as a parm.

The one thing to remember - variable names in a function are only known
within that function. They can't be used outside of it.

Global variables can be used anyplace.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 18 '06 #6

Jerry Stuckle wrote:
No, you can also make it a global in the function and not pass it as a parm.

The one thing to remember - variable names in a function are only known
within that function. They can't be used outside of it.

Global variables can be used anyplace.

OK, I used the global keyword and verified it (via print_r() ) in the
function, but I get nothing when I add print statements to
preg_replace_ca llback() :

$tagpatt = '/\{(\w+?)\}/';
$emesg = preg_replace_ca llback( $tagpatt,
create_function ( '$matches',
'print_r($match es);print_r($in fo);$field=strt olower($matches[1]);return
$info[$field];' ),
$emesg );
It seems like p_r_c() can't do what I want, but it's seems to be
designed for just this type of backreference manipulation. Am I just
going about this the wrong way?

Aug 18 '06 #7
mens libertina wrote:
Jerry Stuckle wrote:
>>No, you can also make it a global in the function and not pass it as a parm.

The one thing to remember - variable names in a function are only known
within that function. They can't be used outside of it.

Global variables can be used anyplace.

OK, I used the global keyword and verified it (via print_r() ) in the
function, but I get nothing when I add print statements to
preg_replace_ca llback() :

$tagpatt = '/\{(\w+?)\}/';
$emesg = preg_replace_ca llback( $tagpatt,
create_function ( '$matches',
'print_r($match es);print_r($in fo);$field=strt olower($matches[1]);return
$info[$field];' ),
$emesg );
It seems like p_r_c() can't do what I want, but it's seems to be
designed for just this type of backreference manipulation. Am I just
going about this the wrong way?
Yes, it can do what you want. The question is - are you getting any
matches? I'm far from an expert on regex's, but it looks like you
aren't getting any matches.

If the code is as you indicate, you should, however. What happens if you
print $tagpart and $emesg?

Also - do you get any output after the call to preg_replace_ca llback
(just to make sure there isn't a runtime error you aren't seeing)?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Aug 18 '06 #8

Jerry Stuckle wrote:
Yes, it can do what you want. The question is - are you getting any
matches? I'm far from an expert on regex's, but it looks like you
aren't getting any matches.

If the code is as you indicate, you should, however. What happens if you
print $tagpart and $emesg?

Also - do you get any output after the call to preg_replace_ca llback
(just to make sure there isn't a runtime error you aren't seeing)?

Here's is my create_function output:

Array ( [0] ={NAME} [1] =NAME ) Array ( [0] ={USERNAME} [1] =>
USERNAME ) Array ( [0] ={EXPIRES} [1] =EXPIRES )

Hello, !

This is a courtesy reminder that your account, , is due to expire on .

Please let us know if you need assistance or have questions.

Thanks,
Staff

Aug 18 '06 #9

mens libertina wrote:
Jerry Stuckle wrote:
Here's is my create_function output:

Array ( [0] ={NAME} [1] =NAME ) Array ( [0] ={USERNAME} [1] =>
USERNAME ) Array ( [0] ={EXPIRES} [1] =EXPIRES )

Hello, !

This is a courtesy reminder that your account, , is due to expire on .

Please let us know if you need assistance or have questions.

Thanks,
Staff
oops, I forgot to mention that the template with the missing tag info
is *after* p_r_c().

thanks for all your help and quick responses!

Aug 18 '06 #10

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

Similar topics

3
2639
by: Ian.H | last post by:
Hi all, I'm trying to write a PHP colour parser and after various attempts, something, somewhere trips it over. Some details: String to parse: $origString = '^1foo^5bar';
0
1031
by: George K | last post by:
This what my program should do, you give it the URL to a page and a template file, it downloads that page and then using the template file it returns some information. The way I thought of doing it was that the template file uses regex and then in my program I just do re.search(template, htmlpage) and this would work but the HTML document has characters like ? and * that I need to escape in the template, so this solution doesn't work....
3
2324
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id `eck<>' for `void blah<int>::eck(int)' does not match any template declaration ttest.cpp:23: syntax error before `{' token ttest.cpp:25: syntax error before `<<' token Note that 'gak' (a template function in a template class) works, as
2
2218
by: littlefitzer | last post by:
Hi, I have come across a tricky little problem, I hope maybe one of you can help. The problem I am having is that I need to parse two seperate values from an XML document using XSL. The two elements however are identical in their names and XPath appearing as below in the XML (I have left out XPath as they are the same): <esb:AcctTypeCode>43</esb:AcctTypeCode> <esb:AcctTypeCode>21</esb:AcctTypeCode>
8
1606
by: jamesfin | last post by:
Dear XMLers, Please guide me... I have a simple xml file... <URLTest>
35
3385
by: .:mmac:. | last post by:
I have a bunch of files (Playlist files for media player) and I am trying to create an automatically generated web page that includes the last 20 or 30 of these files. The files are created every week and are named XX-XX-XX.ASX where the X's represent the date i.e. 05-22-05.asx The files are a specific format and will always contain tags like the following: <TITLE>My media file title</TITLE> <AUTHOR>Media file author</AUTHOR> <Ref href =...
8
4478
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 create_function() to create a make-shift callback function within the method. (seeing as you can't...
2
3573
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) }
6
1925
by: gw7rib | last post by:
I have a program that needs to do a small amount of relatively simple parsing. The routines I've written work fine, but the code using them is a bit long-winded. I therefore had the idea of creating a class to do parsing. It could be used as follows: int a, n, x, y; Parser par; par << string;
0
8987
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.