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

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