Skip to content

Instantly share code, notes, and snippets.

@qutek
Created November 24, 2014 05:52
Show Gist options
  • Save qutek/595d5d58fe1d1e91272f to your computer and use it in GitHub Desktop.
Save qutek/595d5d58fe1d1e91272f to your computer and use it in GitHub Desktop.
[PHP] Replace string to variable with pattern
<?php
/**
* Replace string with pattern
* (example use: for mail templating)
*
* @codes array();
* @pattern string;
*/
$message = 'coba username {username} atau {user_email}';
$codes = array(
'username' => $username,
'user_email' => $email
);
$pattern = '{%s}';
$map = array();
foreach($codes as $var => $value) {
$map[sprintf($pattern, $var)] = $value;
}
$message = strtr($message, $map);
echo $message;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment