日期:2014-05-17 浏览次数:20437 次
//模拟从数据库中读出的数据均将得到:
$mcontent =<<< 'TXT'
dear:{$user['username']}
你购买的商品:{$goods_name},订单号{$goods_no}已下单成功,由于此商品近期销售火爆,请及时付款!
TXT;
//涉及的变量需自行保证存在
$user['username'] = 'body';
$goods_name = 'xxx';
$goods_no = '123';
//方案一 不过有不少人恐惧 eval
$mcontent = eval("return \"$mcontent\";");
echo $mcontent;
//方案二 动态创建函数,格式有点绕人
$f = create_function('$user,$goods_name,$goods_no', "return \"$mcontent\";");
$mcontent = $f($user, $goods_name, $goods_no);
echo $mcontent;
//或
$f = create_function('$user,$goods_name,$goods_no', 'return "' . $mcontent . '";');
$mcontent = $f($user, $goods_name, $goods_no);
echo $mcontent;
//方案三 模板变量替换,这是解释型模板引擎的做法
$mcontent = preg_replace('/{\$[^}]+}/e', '$1', $mcontent);
echo $mcontent;