[FX.php List] Seeking improved "mail merge"

Gareth Evans gareth.evans at schawk.com
Fri Feb 19 09:27:10 MST 2010

This is the kind of thing I do when dealing with user created templates. It
will replace all {tags} with their corresponding value or remove the {tag}
if there is no value for the tag supplied.


$text = 'Dear {firstname} {lastname}.... {tag_with_no_tagValue_supplied}';

$tagValues = array();
$tagValues['firstname'] = 'John';
$tagValues['lastname'] = 'Doe';

echo mergeText($text, $tagValues);

function mergeText($text, $tagValues) {

    preg_match_all('/{(.*?)}/', $text, $matches);
    foreach($matches[1] as $tag) {
        $replaceStr = '';
        if (array_key_exists($tag, $tagValues)) {
            $replaceStr = $tagValues[$tag];
        $text = str_replace('{'.$tag.'}', $replaceStr, $text);
    return $text;

> From: Jonathan Schwartz <jschwartz at exit445.com>
> Reply-To: "FX.php Discussion List" <fx.php_list at mail.iviking.org>
> Date: Fri, 19 Feb 2010 09:26:28 -0500
> To: <fx.php_list at mail.iviking.org>
> Subject: [FX.php List] Seeking improved "mail merge"
> Hi Folks,
> I'm looking to improve on my present technique of embedding variables
> within a text string, such as one might do with an email merge.
> I want to allow the admin end users to create custom email messages
> in text strings, embedding pre-established tags that will resolve to
> actual values, such as : "Dear {firstname},  Your order {OrderNumber}
> has shipped."
> Up to this time, I have hard-coded the $variables in a text string
> and used a series of string replace commands...which wont' work in
> this new environment I wan to create.
> So, I am looking for the right php script to do this substitution,
> given there there is a list of a dozen or so possible variables that
> *could* get substituted into the text string, depending whether or
> not the {tag} is present in the body of the test string created by
> the admin user. Of course, I will have the $variables from the db
> set, so $namefirst will provide a value for {firstname}, etc.
> I've been looking at at all the preg options, and have not been able
> to figure out which way to go.
> In essence, I need a function that scans the email text string for
> known {tags} and substitutes values as they are matched. Here is my
> guess how it could work.  If this approach is OK, the key question is
> which function to use for the "if({tag} is found in the string" part.
> $textbody = "Dear {firstname} {lastname}.....";
> $taglist= {'{firstname}', '{lastname}', ......);
> $tagvalues = ('john', 'smith', ....);
> foreach($taglist)
> {
> if( {tag}  is found in  $textbody, substitute $tagvalue;
> }
> Any help?
> Thx
> Jonathan
> -- 
> Jonathan Schwartz
> Exit 445 Group
> jonathan at exit445.com
> http://www.exit445.com
> 415-370-5011
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.iviking.org/pipermail/fx.php_list/attachments/20100219/f2a833e7/attachment.html

More information about the FX.php_List mailing list