douzhang5199 2018-06-20 16:13
浏览 52
已采纳

如何在数据库中保存的文本中使用PHP中的%variable%等动态变量?

Now i understand i will get down vote or hate about this question but i can't find answer anywhere and maybe i am just using wrong words of search.

I need to make function that will email users when they posts expire on website and text from email will be stored in database and i've seen some others use variables like %username% to get proper username on user who will receive email, but i don't get it how to do it my self.

So example of email template would be this:

Dear %username%
Your post will expire in %expiration% days, if you wish to extend your post please login.
On your dashboard you can manage your posts.

Where %username% will be $username of each user who is author of that post.

So any suggestions how is this done, so i need to search and replace for specific tags like %username%,

Have to mention that email template text will be stored in database field.

  • 写回答

1条回答 默认 最新

  • dpcj32769 2018-06-20 16:18
    关注

    Use str_replace to search for the placeholders within your text, and replace the content with the variables you have suggested.

    str_replace(array('%username', '%expiration%'), array($username, $expiration), $body);
    

    The first 2 parameters of str_replace need to align:

    If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search. The converse would not make sense, though.

    Documentation: http://php.net/manual/en/function.str-replace.php

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

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理