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

如何在数据库中保存的文本中使用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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度