Re: string as file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rick Pasotto wrote:
On Thu, Aug 09, 2007 at 02:39:51PM -0700, Jim Lucas wrote:
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

<?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);

}

?>


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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux