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

Andrew Denman adenman at tmea.org
Fri Feb 19 10:55:59 MST 2010


If you didn't want a special function you could also use preg_replace: http://us.php.net/manual/en/function.preg-replace.php It works very similarly to the FileMaker Substitute() function.

You would have to use the two variables (patterns to match and replacement text) like you were headed down in your original email.

Andrew Denman

From: fx.php_list-bounces at mail.iviking.org [mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Jonathan Schwartz
Sent: Friday, February 19, 2010 8:01 AM
To: FX.php Discussion List
Subject: Re: [FX.php List] Seeking improved "mail merge"

Wow!  I think this is it.  Thanks!

Jonathan

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.
<?php

$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;
}
?>
--
GARETH EVANS


> 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.
>
> CODE GUESS....
>
> $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

_______________________________________________
FX.php_List mailing list
FX.php_List at mail.iviking.org
http://www.iviking.org/mailman/listinfo/fx.php_list



--
Jonathan Schwartz
Exit 445 Group
jonathan at exit445.com
http://www.exit445.com
415-370-5011
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.iviking.org/pipermail/fx.php_list/attachments/20100219/33409966/attachment.html


More information about the FX.php_List mailing list