Replace text template for parameter hash table
Replace text template for parameter hash table
Ex:
$mytext = 'Hi, my name is {%name%}, and my address is {%address%}';
rep_templates($mytext, array('name'=>'Steven', 'address'=>'Rua Beira Mar, 12'));
print $text; //Output: Hi, my name is Steven, and my address is Rua Beira Mar, 12
Other ex.
$row = mysql_fetch_assoc($my_result_from_query);
$mytext = file_get_contents('./text_for_email_template.txt');
tranf_dados($mytext, $row);
sendmail($row['email', 'Subject:Hi mane!', $mytext);
*/
1 2 function rep_templates(&$t, $d){ 3 preg_match_all ( '/{\%(\w*)\%\}/' , $t , $matches ); 4 foreach($matches[1] as $m){ 5 if($d[$m]!=null){ 6 $pattern = "/{\%".$m."\%\}/"; 7 $t = preg_replace( $pattern, $d[$m], $t); 8 } 9 } 10 }