用PHP变量替换分隔符

时间:2011-09-05 05:58:11

标签: php regex templates delimiter

我正在编写一个邮件类,用于提取存储在数据库中的内容,并在将其作为HTML电子邮件发送之前将其加载到模板中。但是,因为每个电子邮件都包含PHP变量和动态内容,所以我决定使用分隔符。因此,而不是内容看起来像:

Hello $username, welcome to the site.

看起来像是:

Hello {{username}}, welcome to the site.

到目前为止,我正在使用这些方法:

function load($name,$content)
{
    // preps the template for HTML
}

function content($template_id)
{
    $template = $this->db->get_where('email_templates',array('id'=>$template_id));
    return $template->content;
}

function new_email($email,$name,$user_type)
{
    $msg = $this->load($name,$this->content(1));
    $this->send($email,'Thanks for your application',$msg,1);
}

我遇到的麻烦是如何将{{variable}}转换为$变量以便它将解析 - 我不希望它只是作为$ username加载到电子邮件模板中。它只是一个使用正则表达式和转义字符串以便它将解析的情况?类似的东西:

$content = str_replace("{{","'.$",$template->content);
$content = str_replace("}}",".'",$template->content);

或者这有缺陷吗?有谁知道最好的事情是什么?

5 个答案:

答案 0 :(得分:3)

我不会创建自己的模板系统,因为那里有现有的模板系统。

最受欢迎的可能是Smarty,但还有一个与您创建的格式相同,即mustache

更新

您的代码存在的问题是,您将{{替换为.$并将其存储在$content变量中,然后将}}替换为.并覆盖此替换的$content变量。

可能的工作解决方案可能是:

if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
  foreach ($m[1] as $i => $varname) {
    $template = str_replace($m[0][$i], sprintf('$%s', $varname), $template);
  }
}

但是你还需要以某种方式eval代码。

答案 1 :(得分:2)

因此,在您的电子邮件模板中将{{variable}}转换为$variable后,您将使用eval将其替换为该变量的实际内容?

为什么不立即将{{variable}}替换为$variable的内容?

也许有一个函数可以获取模板文本和placeholder => "text to replace it with"数组。然后,通过在该数组的键周围添加{{}}并执行str_replace,就像编写占位符的确切字符串一样简单。

foreach ($replacements as $placeholder => $value) {
    $placeholder = "{{" . $placeholder . "}}" ;
    $text = str_replace($placeholder, $value, $text) ;
}

将此与占位符的(类)常量相结合,您就拥有了一个非常可靠且错误重复的模板系统。它不会像完整的模板解决方案一样优雅或易于使用,并且可能需要编写使用它的代码的任何人的额外工作,但是由于错误命名的变量,它们在开发期间不会出错。

答案 2 :(得分:0)

使用preg_replace_callback,请参阅:http://codepad.org/EvzwTqzJ

<?php

$myTemplateStr = "Hello {{username}} , this is {{subject}} ,and other string {{example}}";
$tagRegex = "|{{(.*?)}}|is";
$result = preg_replace_callback($tagRegex,"myReplaceFunc",$myTemplateStr);
echo $result ;

/* output :     

Hello $username , this is $subject ,and other string {{example}}

*/



function myReplaceFunc($matches)
{
  $validTags = array('username','subject','name');
  $theFull = $matches[0];
  $theTag = $matches[1];
  if(in_array($theTag,$validTags) == true)
    return '$'.$theTag;
  return $theFull ;
}

?>

答案 3 :(得分:0)

如果您打算自己动手,最好只使用str_replace进行显式操作。如果你试图将花括号转换为$,那么你需要eval(),这是一个潜在的安全漏洞。

这将是我使用str_replace的方法 - 当你添加更多变量时,这变得很难维护,但它实际上也没有那么简单。

$content = str_replace(
             array('{{username}}','{{var2}}'),
             array($username,$var2),
             $template->content
           );

答案 4 :(得分:0)

$template = "Hello {{username}} , this is {{subject}} ,and other the answer is on page {{example}}";

$replacements = array(
    'username' => 'Jeffrey',
    'subject' => 'your final notice',
    'page' => 43
);


function bind_to_template($replacements, $template) {
    return preg_replace_callback('/{{(.+?)}}/',
             function($matches) use ($replacements) {
        return $replacements[$matches[1]];
    }, $template);
}


echo bind_to_template($replacements, $template);

贷记https://www.labnol.org/code/19266-php-templates