douhu1990 2017-05-19 19:55
浏览 48
已采纳

如何在PHP / MySQL应用程序中创建合并标签?

I'm writing a PHP/MYSQL SaaS app and I'm currently working on email functionality (using Swift). I plan to save email templates to the MySQL database (each tenant will be able to customize them). However, I also need to include dynamic data in the templates ("merge tags" like first name and last name). I tried simply storing {$test} in the DB but that didn't work. Is there something I need to do after pulling the data from MySQL to merge variables into the text before passing it to Swift?

  • 写回答

1条回答 默认 最新

  • douqiju2520 2017-05-19 21:02
    关注

    I'm not familiar with the Swift library, but a quick glance at the documentation didn't turn up anything about parsing messages to insert variables. That means you'll likely need to do that yourself.

    One way you might do it, is to come up with your own (arbitrary) syntax for holding a variable's position in a message. For example, you might want to use {%VAR_NAME%} or something.

    So you can save your templates like:

    Hello {%recipient_name%},

    This is to inform you that invoice #{%invoice_num%} is available as of {%invoice_date%}.

    Thank you, {%sender_name%}

    And then, assuming that template is stored in $email_body, you could do a string replace (either str_replace(), or something like preg_replace(). For something this simple, I wouldn't suggest bogging things down with a regular expression search). Below is an example of how you might go about doing it. You can build the $search and $replace arrays however you would like, but I just laid it out like this to be more human readable.

    // Original body:
    $email_body = "Hello {%recipient_name%},
                   This is to inform you that invoice #{%invoice_num%} is available as of {%invoice_date%}.
                   Thank you, {%sender_name%}";
    
    // Easily readable input values
    $values = array(
        'recipient_name'    => $db_value['recipient_name'],
        'invoice_num'       => $db_value['invoice_num'],
        'invoice_date'      => date("Y-m-d"),
        'sender_name'       => $_SESSION['user_name']
    );
    
    // Break it up into the search and replace arrays
    $search = array();
    $replace = array();
    foreach ($values AS $index => $value)
    {
        $search[] = "{%" . $index . "%}"; // Wrapping the text in "{%...%}"
        $replace[] = $value;
    }
    
    // Do the replacement
    $email_body = str_replace($search, $replace, $email_body);
    

    The main idea here is that you will be replacing placeholders with a specific syntax (eg {%VALUE%}) with a real value. How you generate the $search and $replace variables is up to you.

    This will output something like:

    Hello Foo,

    This is to inform you that invoice #123456 is available as of 2017-05-19.

    Thank you, Bar

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码