1

Totally lost on how to solve. I have a mysql text record which contains the following text:

Hello $someone

When I output it in php, it shows as Hello $someone - which is fine and what I expect.

However, how can I output it in php so that it turns $someone into a php variable, which is assigned in php?... So I'd like my php code like:

$someone = "John Doe";
echo $subject;

returns: Hello John Doe

I've tried looking at variable variables, using $$someone, ${$someone} but always just returns text.

I understand that $someone would always be a text, so would have to have it stored in mysql as something like {$someone} to differentiate it from a dollar amount like $50 etc.

Any help would be greatly appreciated!

5
  • It is a terrible practice to store variable names in database. What will happen when someone changes the variable name in the application ? If you want to store messages that will be dynamically changed by the application, use placeholders. Store the message as Hello <placeholder> and then replace the <placeholder> part of the message with the value of the $someone variable. Commented Jul 1, 2018 at 10:42
  • I did consider that, however it's only me using the app. If anyone else were to use it though, I'd limit it to only allow certain variables... Commented Jul 1, 2018 at 10:50
  • Well it does not matter how many people are going to use the app. What if you decide to do a refactor a few months from now ? You will have to make database migrations in order to update the names of the changed variables. Commented Jul 1, 2018 at 10:57
  • Anyone else? :) Commented Jul 1, 2018 at 11:03
  • You could write a fairly simple function that would substitute placeholders with text in an array. Rather than using a $var syntax, I would suggest using something like {var}. This uses characters that rarely are used, and provides at starting and ending reference for the variable to be substituted. I'll add an answer with a function like that... Commented Jul 1, 2018 at 11:08

1 Answer 1

1

Here's an exampe of a simple function that will replace placeholder values in a string. Both the strings ($text) and the data could easily come from the database.

$text = "";
$text .= "<p>Dear {name},</p>\n";
$text .= "<p>Thank you for your order on {order_date}.</p>\n";
$text .= "<p>You order was shipped on {ship_date}.</p>\n";

$data = array(
    "name" => "John Doe",
    "order_date" => "05/01/2018",
    "ship_date" => "05/04/2018",
    "order_total" => "$22.50"
);

$textToDisplay = curly_replacer($text,$data);
echo $textToDisplay;

//  Function to replace placeholders with data values.
//  $str contains placeholder names enclosed in { }
//  $data is an associative array whose keys are placeholder, 
//  and values are the values to replace the placeholders
function curly_replacer($str,$data) {
    $rslt = $str;
    foreach($data as $key => $val) {
        $rslt = str_replace("{".$key."}", $val, $rslt);
    }
    return $rslt;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.