Re: string as file
| From: | Jim Lucas | Date: | Fri, 10 Aug 2007 16:36:03 +0000 |
| Subject: | Re: string as file | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260605@lists.php.net to get a copy of this message | ||
Rick Pasotto wrote:
On Thu, Aug 09, 2007 at 02:39:51PM -0700, Jim Lucas wrote:<?php $string = 'Hi, my name is |FIRST_NAME| |LAST_NAME|.'; $values = array(); $values[] = array('FIRST_NAME' => 'Jim', 'LAST_NAME' => 'Lucas'); $values[] = array('FIRST_NAME' => 'James', 'LAST_NAME' => 'Lucas'); $values[] = array('FIRST_NAME' => 'Jimmy', 'LAST_NAME' => 'Lucas'); foreach ( $values AS $row ) { $in = array_keys($row); foreach ( $in AS $k => $v ) $in[$k] = '|'.$v.'|'; echo str_replace($in, $row, $string); } ?>Rick Pasotto wrote:Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = <<<EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. Can I someout "include" a string instead of a file? Or maybe there is some completely different way to do what I want.template.php <?php ob_start(); echo "Hi, my name is {$first_name} {$last_name}."; return ob_get_clean(); ?> This is two different ways you can do it, bases on your input data array structure test.php
You have misunderstood. You are still putting the template in an external file. I want it in the main file. I don't want to maintain two different files.-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare