fork download
  1. <?php
  2. $email = 'johndoe@example.com';
  3. $username = 'johndoe';
  4. $to_replace = array( "{username}", "{email}" );
  5. $replace_with = array( $username, $email );
  6.  
  7. $key_value = array(
  8. 'site' => 'abc.com',
  9. 'blog' => 'blog.com',
  10. 'roll' => 42
  11. );
  12.  
  13. $to_replace = array_merge($to_replace, array_keys($key_value));
  14. $replace_with = array_merge($replace_with, array_values($key_value));
  15.  
  16. array_walk($to_replace, function(&$value, $key) { $value = '{'.trim($value, '{}').'}';});
  17.  
  18. $message = 'This is a {username} speaking, my email is {email}, and my site is {site} with roll {roll}';
  19. $message = str_replace( $to_replace, $replace_with, $message );
  20.  
  21. var_dump($message);
Success #stdin #stdout 0.02s 23616KB
stdin
Standard input is empty
stdout
string(96) "This is a johndoe speaking, my email is johndoe@example.com, and my site is abc.com with roll 42"