使用preg_replace_callback()解析模板 [英] parsing template using preg_replace_callback()
问题描述
免责声明:除了阅读php.net上的少量条目外,我已经搜查了这个组,并且alt.php无效。
基本上,我正在尝试使用模板发送电子邮件提醒,但是我不能按照我想要的方式使用我的反馈。 (template.inc不是在我的主机上安装了
。)如果我接近问题是错的,请
有怜悯并告诉我如何解决它! :)
我有一个数组,$ info,看起来像这样(这只是一个用户):
数组([morgan] =数组< br $> b $ b(
[用户] =摩根
[姓名] = Morgan Seppy
[到期日] = 2006-08 -28
)
)
和示例模板文件:
你好,{NAME } *
这是礼节性的提醒,您的帐户{USER}将于{EXPIRES}到期
到期。
如果您需要帮助或有疑问,请告诉我们。
谢谢,
员工
所以,我想用$ info中的相应项替换{TAG}。
相关代码不起作用(我已经删除了我的调试打印
语句):
函数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);
}
}
结果:
警告:在
/ home /中缺少__lambda_func()的参数2 content / c / a / j / cajtech / html / awkwords / admin / remind.php(39):
第1行的运行时创建函数
似乎preg_replace callback()不知道$ info存在。
我也尝试过使用backrefences,但我无法操纵它们
(从内存中写这个):
$ emesg = preg_replace($ tagpatt,eval(" $ info [strtolower("。" $ 1"。")]] ),
$ emesg);
谢谢
在fo,看起来像这样(这只是一个用户):
数组([morgan] =数组
(
[user] =摩根
[姓名] = Morgan Seppy
[到期] = 2006-08-28
)
)
和示例模板文件:
您好,{NAME}!
这是礼貌提醒您的帐户{USER}将于{EXPIRES}到期
到期。
如果您需要帮助或有疑问,请告诉我们。 />
谢谢,
员工
所以,我想用{/}中的相应项替换{TAG} BLOCKQUOTE>
信息。
相关代码不起作用(我已经删除了我的调试打印
语句):
功能CreateEmail(
info)
{
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
解决方案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 )
{
这篇关于使用preg_replace_callback()解析模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!