You are here: parsing template using preg_replace_callback() « PHP Programming Language « IT news, forums, messages
parsing template using preg_replace_callback()

Posted by mens libertina on 08/18/06 00:41

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_contents( TMPLFILE );
if(!$emesg)
{ error_log( "Template file, ".TMPLFILE.", not read.\n", 3,
$errfile ); }
else
{
$tagpatt = '/\{(\w+?)\}/';
$emesg = preg_replace_callback( $tagpatt,
create_function( '$matches, $info',
'$field=strtolower($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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация