Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Steven Koch http://www.surfih.com/surfer/stvkoch

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

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  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS