Created
November 24, 2014 05:52
-
-
Save qutek/595d5d58fe1d1e91272f to your computer and use it in GitHub Desktop.
[PHP] Replace string to variable with pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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