Search code examples
phpmysqlvariables

How to store and retrieve string mix with PHP variable name from MySQL database?


I have a question and try searching on Google and Stakoverflow and could't find any perfect solution. I am using phpmailer and want to let the user to customize it's body message. So I have given and option to edit the email body text by providing a texarea input box in back end where user will input the email text with some my own provided php variable.

For example body text is "Dear {$username}, thank you for signup.". Once I store this input into database and want to retrieve this in php:

$username = "ABC"; //username variable
$message = $row["message"]; //stored email body message
echo $message;

OUTPUT DATA: "Dear {$username}, thank you for signup."

I want to get it like this "Dear ABC, thank you for signup.". How is it possible or is there any other option I can use it with php varialbe or defined type?


Solution

  • I have found a solution in PHP with str_replace function. This function is really very helpful. I have tried with modified input message "Dear user_name, thank you for signup.":

    $username = "ABC"; //username variable
    $message = $row["message"]; //stored email body message
    $message_new = str_replace("user_name", $username, $message);
    echo $message_new;
    

    OUTPUT DATA: "Dear ABC, thank you for signup."