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

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

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果