I can't think of how to do this in my head so I would like someone to lay this out for me. What I am trying to do is allow a user to set a custom greeting for a email using a variable. So for example, in the body of the email, they'll place "$greeting" which will echo out a random greeting they set {'hey', 'hello', 'hi'}.
can someone explain how I would achieve this?
define your possible greetings in an array
$greetings = array('hey', 'hello', 'hi');
echo out a random one
echo $greetings[array_rand($greetings)];
or
shuffle($greetings);
echo $greetings[0];
you will need to use something like https://github.com/bobthecow/mustache.php
its the cleanest way, regex is not the way to do it. that would allow you to do something like
hi {user.name}, ... in the mail or any other vars that need to be customized
Ask the user to supply a comma seperated list of greetings in a text input:
hey, hello, hi
You can then explode the greetings via , then trim the whitespace (or the other way around probably)
Then allow them to put the $greeting in the message and use array_rand to pick a random one
$input = 'hi, heya, morning!';
$greetings = explode(',', str_replace(' ', '', $input));
$key = array_rand($greetings);
echo $greetings[$key];
Something like this maybe is what you want to do:
$userText="A user wants to send a greeting: \$greeting";
echo $userText;
echo "<br/><br/>";
$valArray=array("Hey","Hello","Ciao");
$newText=str_replace("\$greeting",$valArray[array_rand($valArray)],$userText);
echo $newText;