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

Chris Hansen chris at iViking.org
Fri Feb 19 11:53:04 MST 2010


Jonathan,

Here's an approach I haven't seen others suggest yet...  In the  
current case, I'd prefer to use a regular str_replace() than use a  
regular expression for performance sake.  And, if you are all like me,  
you'd love to manage this from the database side.  So, what you'd want  
is a database where one field contained the possible tags, and another  
field which contains the name of the variable that would contain the  
value.  Access these using a portal that contains the desired "tag"  
records (a portal works for my suggestion, whereas a found set of  
records does not...)  Then do this:

$emailBody = $longBitOfTextWithTags;
...Set your replace variables as needed...
...FX Query goes here...
$searchArray = $queryResultsArray['relationshipName::tagFieldName'];
$replaceArray =  
compact($queryResultsArray['relationshipName::variableNameFieldName']);
$emailBody = str_replace($searchArray, $replaceArray, $emailBody);

Hopefully the above isn't too abstract.  Not a lot of code, and should  
be quite speedy.  Some of my favorite things about PHP.

HTH

--Chris

On Feb 19, 2010, at 7:26 AM, Jonathan Schwartz wrote:

> 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
>




More information about the FX.php_List mailing list